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

后端 未结 10 881
暖寄归人
暖寄归人 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:50

    I think Robin Maben's solution is missing one step which is to set the boxes to have a property of 'checked'. jQuery's user guide notes on serialise() state that only checked checkboxes are serialised, so we need to add a bit:

    $('form').find(':checkbox:not(:checked)').attr('value', '0').prop('checked', true);
    

    The only down-side to this method is a brief flash of your form showing all boxes checked - looks a bit weird.

提交回复
热议问题