This question already has an answer here:
- Post the checkboxes that are unchecked 34 answers
I have the following piece of code:
var contract_tax = $('#contract_tax');
var contract_inflation = $('#contract_inflation');
contract_tax.on('change', function () {
contract_tax.val(this.checked ? 1 : 0);
});
contract_inflation.on('change', function () {
contract_inflation.val(this.checked ? 1 : 0);
});
This way I change the value of the checkbox to 0|1 whether is checked or not. Then I am trying to send all the form content trough AJAX as follow:
$.ajax({
method: 'POST',
url: '/update',
data: {item_id: item_id, data: $('#add_form').serializeArray()},
...
})
The problem is I am using .serializeArray() for send values trough AJAX to the backend but it's not sending those checkbox that aren't checked. I did read on .serialize() docs the following:
Values from checkboxes and radio buttons (inputs of type "radio" or "checkbox") are included only if they are checked. Data from file select elements is not serialized.
so, how can I send all the form values despite of the rules? (meaning I want to send the unchecked checkboxes as well). Do I need to get this one by one? Any better way?
Aucun commentaire:
Enregistrer un commentaire