I was wondering how is it popssible to populate forms using JSON?

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-plugin),我怎么能实现这样的事情呢.

EDIT: JSON format:

[{"id":"41","parent_id":null,"node_name":"name","slug":"","lft":"3","rgt":"4"}]

这是我从json_encode()得到的信息

推荐答案

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); 
        }  
    });  
}

Json相关问答推荐

使用Jolt将字符串数组转换为JSON对象数组

如何在Vega中使标记的符号在鼠标指针悬停时可点击

有没有办法让serde_json正确/不正确地处理NaN、inf和-inf(IEEE 754特殊标准)?

Jolt-在数组列表中插入新的字段

使用 JOLT 将日期格式转换为 JSON

使用杰克逊解析Kotlin 中的通用密封类

使用 json 值过滤 Django 模型

JSON 模式实际用于什么目的?

从 json 对象中过滤掉所有出现的给定属性

无法向 Json 数组添加新元素

Golang / Go - 如果 struct 没有字段,如何将其编组为空?

将 YAML 文件转换为 Python JSON 对象

在android中读取Json数组

将序列化表单的数据转换为 json 对象

Sequelize - 如何仅返回数据库结果的 JSON 对象?

如何使用 Gson 解码具有未知字段的 JSON?

有没有办法折叠 Postman 中的所有 json 字段

Python 到 JSON 序列化在十进制上失败

如何在 postgresql 9.3 中循环 JSON 数组

log4j 支持 JSON 格式吗?