form表单提交参数封装

function getFormValues(element,options) {
var data = {};
if(element == null || element == undefined) {
return data;
}
/** default options */
if (!options) { options = {checkBoxTrueValue:1,checkBoxFalseValue:0}; }
var trueValue = options['checkBoxTrueValue'];
var falseValue = options['checkBoxFalseValue'];

var elementsObj = element.get(0).elements;
var obj;
if (elementsObj) {
for ( var i = 0; i < elementsObj.length; i += 1) {
obj = elementsObj[i];
if (obj.tagName == "INPUT" || obj.tagName == "SELECT") {
if(obj.type == 'checkbox' || obj.type == 'radio'){
if($(obj).is(':checked')){
try{
if(options[obj.name+"_trueValue"] != undefined){
trueValue = options[obj.name+"_trueValue"];
}
}catch(e){

}
data[obj.name] = trueValue;
}else{
try{
if(options[obj.name+"_falseValue"] != undefined){
falseValue = options[obj.name+"_falseValue"];
}
}catch(e){

}
data[obj.name] = falseValue;
}
continue;
}
if(obj.name == "id" && obj.value == "") {

} else {
data[obj.name] = obj.value;
}
}
}
return data;
}
}

posted @ 2019-03-04 19:42  本木木  阅读(2063)  评论(0编辑  收藏  举报