jquery serialize表单序列化,当radio或checkbox 未选中时,没有序列化到对

2020-07-11 18:07 2023-12-04 15:15 标签:jquery

//为jquery.serializeArray()解决radio,checkbox未选中时没有序列化的问题
    $.fn.ghostsf_serialize = function () {
        var a = this.serializeArray();
        var $radio = $('input[type=radio],input[type=checkbox]', this);
        var temp = {};
        $.each($radio, function () {
            if (!temp.hasOwnProperty(this.name)) {
                if ($("input[name='" + this.name + "']:checked").length == 0) {
                    temp[this.name] = "";
                    a.push({name: this.name, value: ""});
                }
            }
        });
        //console.log(a);
        return jQuery.param(a);
    };


怎么使用呢?引入即可  

$(form).ghostsf_serialize()


压缩解压