Using jQuery and JSON to populate forms?

前端 未结 10 1086
忘掉有多难
忘掉有多难 2020-12-04 10:41

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

10条回答
  •  无人及你
    2020-12-04 11:11

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

提交回复
热议问题