how can I override jquery's .serialize to include unchecked checkboxes

后端 未结 10 843
暖寄归人
暖寄归人 2020-12-16 14:21

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 $.serial

相关标签:
10条回答
  • 2020-12-16 14:57

    This will override the jquery.serialize() method to send both checked/unchecked values, rather than only checked values. It uses true/false, but you can change the "this.checked" to "this.checked ? 'on' : 0" if you prefer.

    var originalSerializeArray = $.fn.serializeArray;
    $.fn.extend({
        serializeArray: function () {
            var brokenSerialization = originalSerializeArray.apply(this);
            var checkboxValues = $(this).find('input[type=checkbox]').map(function () {
                return { 'name': this.name, 'value': this.checked };
            }).get();
            var checkboxKeys = $.map(checkboxValues, function (element) { return element.name; });
            var withoutCheckboxes = $.grep(brokenSerialization, function (element) {
                return $.inArray(element.name, checkboxKeys) == -1;
            });
    
            return $.merge(withoutCheckboxes, checkboxValues);
        }
    });
    
    0 讨论(0)
  • 2020-12-16 15:00

    Here's how I implemented a simple override of $.serializeArray which fixes the default serialization behaviour for checkboxes, and default behaviour is retained for all other types.

    In the code below, missed checkboxes are injected into the original serialized array. Checkbox state is returned as "true" (instead of "on") or "false" depending on if it is checked or not.

    (function ($) {
        var _base_serializeArray = $.fn.serializeArray;
        $.fn.serializeArray = function () {
            var a = _base_serializeArray.apply(this);
            $.each(this, function (i, e) {
                if (e.type == "checkbox") {
                    e.checked 
                    ? a[i].value = "true" 
                    : a.splice(i, 0, { name: e.name, value: "false" })
                }
            });
            return a;
        };
    })(jQuery);
    

    You could customize this to return "on"/"off" or true/false.

    0 讨论(0)
  • 2020-12-16 15:01

    just attach the data. In my save routine it's enough to submit unchecked as empty string and checked as "on":

    var formData = $('form').serialize();
    
    // include unchecked checkboxes. use filter to only include unchecked boxes.
    $.each($('form input[type=checkbox]')
        .filter(function(idx){
            return $(this).prop('checked') === false
        }),
        function(idx, el){
            // attach matched element names to the formData with a chosen value.
            var emptyVal = "";
            formData += '&' + $(el).attr('name') + '=' + emptyVal;
        }
    );
    
    0 讨论(0)
  • 2020-12-16 15:03

    Before you call serialize(), you need to explicitly set the value to false if a checkbox is unchecked

    $(form).find(':checkbox:not(:checked)').attr('value', false);
    

    I have tested it here.

    0 讨论(0)
提交回复
热议问题