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

本文介绍了一种通过Ajax方式提交Form表单的方法,并提供了一个具体的实现案例。该方法能够将表单序列化为JSON对象,便于后台进行处理。

       需要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,如需转载请自行联系原作者

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值