/**
* 将form表单元素的值序列化成对象
* Exp: extUtil.serializeObject($('#formID').form());
*/
extUtil.serializeObject = function (form) {
var o = {};
$.each(form.serializeArray(), function (index) {
if (o[this['name']]) {
o[this['name']] = o[this['name']] + "," + this['value'];
} else {
o[this['name']] = this['value'];
}
});
return o;
};
//将后台Json数据填充表单(Jquery自定义方法)
$.fn.populateForm = function(data){
return this.each(function(){
var formElem, name;
if(data == null){this.reset(); return; }
for(var i = 0; i < this.length; i++){
formElem = this.elements[i];
//checkbox的name可能是name[]数组形式
name = (formElem.type == "checkbox")? formElem.name.replace(/(.+)\[\]$/, "$1") : formElem.name;
if(data[name] == undefined) continue;
switch(formElem.type){
case "checkbox":
if(data[name] == ""){
formElem.checked = false;
}else{
//数组查找元素
if(data[name].indexOf(formElem.value) > -1){
formElem.checked = true;
}else{
formElem.checked = false;
}
}
break;
case "radio":
if(data[name] == ""){
formElem.checked = false;
}else if(formElem.value == data[name]){
formElem.checked = true;
}
break;
case "button": break;
default:
formElem.className.indexOf("formatMoney") != -1
? formElem.value = outputMoney(data[name]) : formElem.value = data[name];
}
}
});
};