首页 > 代码库 > Js 表单与对象相互绑定
Js 表单与对象相互绑定
function GetFormData(formId) {
//获取表单数据
var postData = http://www.mamicode.com/$("#" + formId).serializeArray();
var data = http://www.mamicode.com/[];
for (var i = 0; i < postData.length; i++) {
var IsAdd = true;
for (var j = 0; j < data.length; j++) {
if (data[j].name == postData[i].name) {
data[j].value += "%&" + postData[i].value//重复键用"%&"隔开
IsAdd = false;
break;
}
}
if (IsAdd) {
data.push(postData[i]);
}
}
return data;
}
function BindForm(formId, data) {//绑定数据给表单
for (items in data) {
var dd = $("#" + formId + " [name^=‘" + items + "‘]");
if (dd != null) {
var ty = dd.attr("type");
if (ty != null && ty == "radio") {
dd.removeAttr("CHECKED");
$("[name=" + items + "][value="http://www.mamicode.com/+ data[items] +"]").attr("checked", true);
} else if (ty != null && ty == "checkbox") {
dd.removeAttr("CHECKED");
var strdata = http://www.mamicode.com/data[items].split("%&");
for (var k = 0; k < strdata.length; k++) {
$("[name=" + items + "][value="http://www.mamicode.com/+ strdata[k] +"]").attr("checked", true);
}
} else {
dd.val(data[items]);
}
}
}
}
Js 表单与对象相互绑定