jquery 将表单序列化成json对象 以便post提交参数

2019-06-23  本文已影响0人  eye33

以下方法写到js中,然后表单对象调用该方法,即可返回json对象

$.fn.serializeJSON = function()
{
    var o = {};
    var a = this.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;
};

JSON对象转化json字符串,可以alert()出来观察

JSON.stringify(jsonData)
上一篇下一篇

猜你喜欢

热点阅读