如何覆盖 jquery 的 .serialize 以包含未选中的复选框 [英] how can I override jquery's .serialize to include unchecked checkboxes

查看:9
本文介绍了如何覆盖 jquery 的 .serialize 以包含未选中的复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了很多将 html 复选框发布到服务器的不同方法,但我真的希望在不修改任何内容的情况下做到这一点,除了 $.serialize.理想情况下,我希望选中的框发布为 on,未选中的框发布为 0、空或空.

I have read quite a few different methods of having html checkboxes get posted to the server, but I am really looking to do it without modifying anything except for $.serialize. I ideally, I would like checked boxes to be posted as on, and unchecked to be posted as 0, empty, or null.

我对 jquery 的内部工作有点困惑,但到目前为止我已经做到了,但它将未选中的复选框设置为打开"...谁能告诉我如何在下面继续进行此修改?

I'm a little confused by jquery's inner-workings, but I've got this so far, but it sets unchecked checkboxes to 'on'... Can anyone tell me how to continue this modification below?

$.fn.extend({
    serializeArray: function() {
        return this.map(function(){
            return this.elements ? jQuery.makeArray( this.elements ) : this;
        })
        .filter(function(){
            return this.name && !this.disabled &&
                ( this.checked || !this.checked || rselectTextarea.test( this.nodeName ) || rinput.test( this.type ) );
        })
        .map(function( i, elem ){
            var val = jQuery( this ).val();

            return val == null ?
                null :
                jQuery.isArray( val ) ?
                    jQuery.map( val, function( val, i ){
                        return { name: elem.name, value: val.replace( /
?
/g, "
" ) };
                    }) :
                    { name: elem.name, value: val.replace( /
?
/g, "
" ) };
        }).get();
    }
});

推荐答案

这是一个小改动:

    .map(function( i, elem ){
        var val = jQuery( this ).val();
        if ((jQuery(this).checked != undefined) && (!jQuery(this).checked)) {
            val = "false";
        }

我还没有测试过,但这似乎是最合乎逻辑的方法.祝你好运

I haven't tested but it seems the most logical approach. Good luck

这篇关于如何覆盖 jquery 的 .serialize 以包含未选中的复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆