使用jQuery和JSON填充表单? [英] Using jQuery and JSON to populate forms?

查看:69
本文介绍了使用jQuery和JSON填充表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆