美文网首页
jquery 将表单序列化成json对象 以便post提交参数

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

作者: eye33 | 来源:发表于2019-06-23 10:40 被阅读0次

    以下方法写到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)
    

    相关文章

      网友评论

          本文标题:jquery 将表单序列化成json对象 以便post提交参数

          本文链接:https://www.haomeiwen.com/subject/wgliqctx.html