在使用ajax处理相关数据时,用到了jquery获得所有表单项目的数据内容并进行传递给对应动态页面处理。
以下是相关的全部演示代码jquery表单数据序列化的各种问题与解决方案
以及对于serialize与serializeArray的区别分析
在用jquery获得checkbox表单相关数据时会有bug,我也对其中的bug给了演示,方便观看bug的效果,然后给出了对应解决方案
以下是演示代码产生的console.log反馈
以下是jquery获得表单里面的表单内容数据的代码
<!DOCTYPE HTML>
<html lang="zh-CN">
<head>
<title>深山在线表单系统</title>
<script language="javascript" type="text/javascript" src="jquery.js"></script>
<script language="javascript" type="text/javascript">
function qwbm_save_biaodan(){
var form_alldata = $('#qwbm_biaodan_form1').serialize();
console.log(form_alldata);
console.log(JSON.stringify(form_alldata));
//这个代码只要遇到checkbox表单项目时就会出错,导致只能获得最后一个项目内容
var d = {};
var t = $('#qwbm_biaodan_form1').serializeArray();
//t的值为[{name: "a1", value: "xx"},
$.each(t, function(){
d[this.name] = this.value;
});
console.log(d);
//处理checkbox表单项目时,会将对应内容归入数组
var o = {};
var a = $('#qwbm_biaodan_form1').serializeArray();
$.each(a, function () {
if (o[this.name] !== undefined) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
console.log(o);
console.log(JSON.stringify(o));
var form_alldata = JSON.stringify(serializeObject($("#qwbm_biaodan_form1")));
console.log(form_alldata);
}
//序列化form表单
function serializeObject(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;
}
</script>
</head>
<body>
<form id="qwbm_biaodan_form1" name="qwbm_biaodan_form1" method="post" action="#">
<input class='bd_it_input1' name="qwbm_e3a6db9d6fb72b99" type="text" id="qwbm_e3a6db9d6fb72b99" value="" maxlength="5" placeholder="填写你的名称">
<textarea class='bd_it_textarea1' name="qwbm_omhrmiot" id="qwbm_omhrmiot" value="" placeholder="请填写尽量多的内容" style=""></textarea>
<ul><li><label for="qwbm_kicmidkp1"><input type="radio" name="qwbm_kicmidkp" id="qwbm_kicmidkp1" value="第一个项目"><em>第一个项目</em></label></li><li><label for="qwbm_kicmidkp2"><input type="radio" name="qwbm_kicmidkp" id="qwbm_kicmidkp2" value="第二个项目"><em>第二个项目</em></label></li><li><label for="qwbm_kicmidkp3"><input type="radio" name="qwbm_kicmidkp" id="qwbm_kicmidkp3" value="第三个项目"><em>第三个项目</em></label></li><li><label for="qwbm_kicmidkp4"><input type="radio" name="qwbm_kicmidkp" id="qwbm_kicmidkp4" value="第四个项目" checked='checked'><em>第四个项目</em></label></li><li><label for="qwbm_kicmidkp5"><input type="radio" name="qwbm_kicmidkp" id="qwbm_kicmidkp5" value="第五个项目"><em>第五个项目</em></label></li></ul>
<ul class='bd_it_checkulbox bd_it_checkulonefonttr'><li><label for="qwbm_jhcmhdjo1"><input type="checkbox" name="qwbm_jhcmhdjo" id="qwbm_jhcmhdjo1" value="汽车"><em>汽车</em></label></li><li><label for="qwbm_jhcmhdjo2"><input type="checkbox" name="qwbm_jhcmhdjo" id="qwbm_jhcmhdjo2" value="飞机"><em>飞机</em></label></li><li><label for="qwbm_jhcmhdjo3"><input type="checkbox" name="qwbm_jhcmhdjo" id="qwbm_jhcmhdjo3" value="飞船" checked='checked'><em>飞船</em></label></li><li><label for="qwbm_jhcmhdjo4"><input type="checkbox" name="qwbm_jhcmhdjo" id="qwbm_jhcmhdjo4" value="自行车"><em>自行车</em></label></li><li><label for="qwbm_jhcmhdjo5"><input type="checkbox" name="qwbm_jhcmhdjo" id="qwbm_jhcmhdjo5" value="轿车" checked='checked'><em>轿车</em></label></li><li><label for="qwbm_jhcmhdjo6"><input type="checkbox" name="qwbm_jhcmhdjo" id="qwbm_jhcmhdjo6" value="海军"><em>海军</em></label></li><li><label for="qwbm_jhcmhdjo7"><input type="checkbox" name="qwbm_jhcmhdjo" id="qwbm_jhcmhdjo7" value="摩托车"><em>摩托车</em></label></li><li><label for="qwbm_jhcmhdjo8"><input type="checkbox" name="qwbm_jhcmhdjo" id="qwbm_jhcmhdjo8" value="飞碟"><em>飞碟</em></label></li><li><label for="qwbm_jhcmhdjo9"><input type="checkbox" name="qwbm_jhcmhdjo" id="qwbm_jhcmhdjo9" value="导弹"><em>导弹</em></label></li></ul>
<select name="qwbm_igzkfbin" id="qwbm_igzkfbin" class='bd_it_selectulbox'><option value="第 1 个产品好">第 1 个产品好</option><option value="第 2 个产品好">第 2 个产品好</option><option value="第 3 个产品好">第 3 个产品好</option><option value="第 4 个产品好" selected='selected'>第 4 个产品好</option><option value="第 5 个产品好">第 5 个产品好</option></select>
<input class='bd_it_submit1' type='button' value="提交保存数据" onclick="qwbm_save_biaodan();" />
</form>
</body>
</html>