$.fn.extend({
getform: function () {
var obj = {};
var array = $(this).serializeArray();
$.each(array, function () {
obj[this.name] = this.value;
});
return obj;
},
setform: function (jsonValue) {
if(typeof(jsonValue) != "undefined"){
var obj = this;
$.each(jsonValue, function (name, ival) {
var $input = obj.find("input:[name=" + name + "]");
if ($input.attr("type") == "radio" || $input.attr("type") == "checkbox") {
$input.each(function () {
if (Object.prototype.toString.apply(ival) == '[object String]') {
var arr= new Array();
arr= ival.split(",");
for (var i = 0; i < arr.length; i++) {
if ($(this).val() == arr[i]){
$(this).attr("checked", "checked");
}
}
} else {
if ($(this).val() == ival){
$(this).attr("checked", "checked");
}
}
});
} else if ($input.attr("type") == "textarea") {
obj.find("[name=" + name + "]").html(ival);
} else {
obj.find("[name=" + name + "]").val(ival);
}
});
}
},
disform: function (jsonValue) {
var obj = this;
$("#"+obj[0].id+" input").each(function () {
this.disabled=true
this.style.backgroundColor = "#dcdcdc";
})
$("#"+obj[0].id+" textarea").each(function () {
this.disabled=true
this.style.backgroundColor = "#dcdcdc";
})
$("#"+obj[0].id+" select").each(function () {
this.disabled=true
this.style.backgroundColor = "#dcdcdc";
})
if(jsonValue!=''){
$(jsonValue+" input").each(function () {
this.disabled=false
this.style.backgroundColor = "#ffffff";
})
$(jsonValue+" textarea").each(function () {
this.disabled=false
this.style.backgroundColor = "#ffffff";
})
$(jsonValue+" select").each(function () {
this.disabled=false
this.style.backgroundColor = "#ffffff";
})
}
},
disformtrue: function () {
var obj = this;
$("#"+obj[0].id+" input").each(function () {
this.disabled=false
this.style.backgroundColor = "#ffffff";
})
$("#"+obj[0].id+" textarea").each(function () {
this.disabled=false
this.style.backgroundColor = "#ffffff";
})
$("#"+obj[0].id+" select").each(function () {
this.disabled=false
this.style.backgroundColor = "#ffffff";
})
}
});
要求json 格式: {"name":"1","sax":"2","checkbox":"1,2,3"}