序列化form表单


// 序列化JSON字符串
$.fn.serializeObject = function () {
    let o = {};
    let a = this.serializeArray();
    $.each(a, function () {
        let value = this.value;
        let chain = this.name.split(".");
        let c = o;
        $.each(chain, function (i, e) {
            if (i === (chain.length - 1)) {
                if (c[e]) {
                    if (!c[e].push) {
                        c[e] = [c[e]];
                    }
                    c[e].push(value || '');
                } else {
                    c[e] = value || '';
                }
            } else {
                if (!c[e]) {
                    c[e] = {};
                }
            }
            c = o[e];
        });
    });
    return o;
};

猜你喜欢

转载自www.cnblogs.com/lovellll/p/10207944.html