How can I serializeArray for unchecked checkboxes?

前端 未结 12 856
耶瑟儿~
耶瑟儿~ 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条回答
  •  你的背包
    2021-02-05 08:21

    you can add a hidden false value for every checkbox:

    
    
    

    You will only get "false" values for unchecked checkboxes and both "true" and "false" for checked checkboxes, so you can remove the duplicates like this:

    var params = {};
    $.each($('form').serializeArray(), function (index, value) {
        params[value.name] = params[value.name] ? params[value.name] || value.value : value.value;
    });
    console.log(params); // outputs: {"Check01":"false","Check02":"true"}
    

提交回复
热议问题