How can I serializeArray for unchecked checkboxes?

前端 未结 12 827
耶瑟儿~
耶瑟儿~ 2021-02-05 08:01

How can I modify this example so it can get values from checkboxes that aren\'t checked?

I want all checkboxes to have a value, if it hasn\'t been checked I want to get

12条回答
  •  -上瘾入骨i
    2021-02-05 08:14

    It's probably easiest to just do it yourself:

     var serialized = $('input:checkbox').map(function() {
       return { name: this.name, value: this.checked ? this.value : "false" };
     });
    

    If there are other inputs, then you could serialize the form, and then find the unchecked checkboxes with something like the above and append that result to the first array.

提交回复
热议问题