使用jQuery和JSON填充表单? [英] Using jQuery and JSON to populate forms?
问题描述
我想知道如何使用JSON填充表单吗?
I was wondering how is it popssible to populate forms using JSON?
我有一个JSON字符串,该字符串是使用php的json_encode()
而且我想使用JSON字符串填充表单控件(例如textarea或文本输入).
I have a JSON string which I get using php's json_encode()
And I want to use the JSON string to populate form controls (such as textarea or text input).
如何在不使用外部插件(例如我看到的jQuery populate插件)的情况下实现这一目标.
How can I achieve such thing without using external plugins (like jQuery populate plugin, which I saw).
JSON格式:
[{"id":"41","parent_id":null,"node_name":"name","slug":"","lft":"3","rgt":"4"}]
这是我从json_encode()中得到的
This is what I get from json_encode()
推荐答案
textarea
在这里有问题,然后将其更改为default
开关值
There is a problem here with textarea
, then I change it to a default
switch value
使用此方法将值分配给许多控件":
Use this to assign values to Many Controls :
function populate(frm, data) {
$.each(data, function(key, value) {
var ctrl = $('[name='+key+']', frm);
switch(ctrl.prop("type")) {
case "radio": case "checkbox":
ctrl.each(function() {
if($(this).attr('value') == value) $(this).attr("checked",value);
});
break;
default:
ctrl.val(value);
}
});
}
这篇关于使用jQuery和JSON填充表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!