JS表单设置值

 //表单设置值
    $.fn.setForm = function(jsonValue) {
        var obj = this;
        $.each(jsonValue, function (name, ival) {
            var $oinput = obj.find("[name=" + name + "]");
            if ($oinput.attr("type") == "radio" || $oinput.attr("type") == "checkbox") {
                $oinput.each(function() {
                    if (Object.prototype.toString.apply(ival) == '[object Array]') { //是复选框,并且是数组  
                        for (var i = 0; i < ival.length; i++) {
                            if ($(this).val() == ival[i]) //或者文本相等
                                $(this).prop("checked", true);
                        }
                    } else {
                        if ($(this).val() == ival)
                            $(this).prop("checked", true);
                    }
                });
            } else if ($oinput.attr("type") == "textarea") { //多行文本框  
                obj.find("[name=" + name + "]").html(ival);
            } else {
                obj.find("[name=" + name + "]").val(ival);
            }
        });
    }
$("form").setForm(row);

//只要form表单里设置的name和数据name一样则数据就可以赋值上去了,减少了大量的DOM操作


原文地址:https://www.cnblogs.com/yzenet/p/5663005.html