需要form表单提交,大表单对字段后台人工处理太麻烦。还是选择form表单对象(entity.xx)提交方便,那么怎么ajax提交这样的form对象表单呢?
命名jquery.commons.js内容如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
/** * 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;
} |
实例如下
1
2
3
4
5
6
7
8
9
|
var myForm = getFormJson($( "#myform" ));
$.ajax({ url : 'saveAppoint.action' ,
type : 'POST' ,
data : myForm,
success : function (data) {
showMsg(data);
}
}); |
form表单内容
1
2
3
4
5
|
< 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后台
1
2
3
4
|
private AppointMent appointment;
public void setAppointment(){...}
public AppointMent getAppointment(){...}
|
即可实现对象表单提交支持。
本文转自 沫沫金 51CTO博客,原文链接:http://blog.51cto.com/zl0828/1731739,如需转载请自行联系原作者