需要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(){...}
即可实现对象表单提交支持。
以上,谢谢!
作者:沫沫金