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

后端 未结 10 879
暖寄归人
暖寄归人 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 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;
        }
    );
    

提交回复
热议问题