您的位置:首页 > Web前端 > JQuery

沫沫金 - jQuery序列化form表单【支持ajax提交form对象表单entity.xxx】

2016-01-05 15:55 906 查看
需要form表单提交,大表单对字段后台人工处理太麻烦。还是选择form表单对象(entity.xx)提交方便,那么怎么ajax提交这样的form对象表单呢?

命名jquery.commons.js内容如下
/**
* FORM对象表单ajax提交前数据处理方法
* @param frm
* @returns JSON Object
*/
function getFormJson(frm) {
var o = {};
var a = $(frm).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 || '';
}
});
return o;
}
实例如下
var myForm = getFormJson($("#myform"));
$.ajax({
url : 'saveAppoint.action',
type : 'POST',
data : myForm,
success : function(data) {
showMsg(data);
}
});
form表单内容
<form id="myform">
<input type="hidden" id="timeType" name="appointment.timeType" readonly="readonly" value="<s:property value="appointment.timeType"/>" />
<input type="hidden" id="visitBegin" name="appointment.visitBegin" readonly="readonly" value="<s:property value="appointment.visitBegin"/>" />
<input type="hidden" id="visitEnd" name="appointment.visitEnd" readonly="readonly" value="<s:property value="appointment.visitEnd"/>" />
</form>
action后台
private AppointMent appointment;

public void setAppointment(){...}
public AppointMent getAppointment(){...}


即可实现对象表单提交支持。

以上,谢谢!
作者:沫沫金

本文出自 “沫沫金的IT心得与技巧” 博客,转载请与作者联系!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: