jquery表单序列化

1.序列化
jQuery:https://cdn.bootcss.com/jquery/3.4.1/jquery.js

var obj1 = $('#queryForm').serialize();//url字符串拼接
var obj2 = $('#queryForm').serializeArray();//对象数组[{name:name,value:value},···]
var obj3 = $('#queryForm').serializeObject();//对象
var obj4 = JSON.stringify(obj3);   //json字符串

$.fn.serializeObject = function() {
    var o = {};
    var a = this.serializeArray();
    $.each(a, function() {
        if (o[this.name]) {
            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;
};

2.ajax提交

$.ajax({
    type: 'POST',
    url: ctx + "/buyOrderDetail/buyOrderDetailSave", 
    async: false,
    data: $('form').serializeObject(),
    success: function(data){
    },
    error:function(data){
    }
});

你可能感兴趣的:(jquery表单序列化)