Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery - SerializeArray() checkbox that is not "checked"

so I am trying to implement the Jquery .serializeArray() method to transform a form into a JSON string to send it out in a request. This works great for my form except for checkboxes and radio buttons. The only time this works properly is when they are checked. Sometimes, I need to know if they are unchecked. They still need to get serialized.

I suppose I could manually loop through the form and grab the values and build the JSON object, but that would not be optimal.

According to the Jquery documentation found here: Jquery Docs anything that fits the W3 standards for a successful control found here should get included. Unfortunately, this does not include checkboxes that are not checked. Does anyone have a work around for this behavior? Thanks in advance...


var form = document.getElementById('f');
console.log($(form).serializeArray();

That spits out the serialized form with the checkboxes that are not checked excluded...

like image 791
gabaum10 Avatar asked Nov 04 '25 16:11

gabaum10


1 Answers

If you really want to use checkboxes and radio buttons, have those input fields update a corresponding hidden field. That way, the hidden input will always be sent.

like image 125
Rolando Cruz Avatar answered Nov 07 '25 11:11

Rolando Cruz