比如:Form中有如下的HTML片段:
Favorite Food:<br/>
Steak:<input type="checkbox" name="food" value="Steak"/><br/>
Pizza:<input type="checkbox" name="food" value="Pizza"/><br/>
Chicken:<input type="checkbox" name="food" value="Chicken"/><br/>
通过jq函数$.param得到的是food=Steak&food=Pizza&food=Chicken,可是我想得到形如food=Steak,Pizza,Chicken又该怎么处理呢?查了下API,可以借助jq的serializeArray函数
$.fn.serializeObject = function() {
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if (o[this.name]) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
o[this.name] = o[this.name].join(",");
} else {
o[this.name] = this.value || '';
}
});
return o;
};
用数组join,避免拼接字符串问题