How can I serializeArray for unchecked checkboxes?

前端 未结 12 845
耶瑟儿~
耶瑟儿~ 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:31

    You can append unchecked checkbox data to .serializeArray result:

    var formData = $("#mybaseelement").serializeArray();
    $('#mybaseelement input[type="checkbox"]:not(:checked)').each(function(i, e) {
        formData.push({name: e.getAttribute("name"), value: false});
    });
    

    This is the least invasive solution I can come up with.

提交回复
热议问题