序列化form表单

javascript

浏览数:212

2019-1-7

// 序列化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;
};