FormData and checkboxes

Currently, when creating a FormData object, the checked flag has been added with the value "on", and the unchecked flag is not passed at all.

Do I need to hack some hidden inputs in order to correctly check the boxes, or is there some kind of setting that I can do with FormData or preprocessing?

I would prefer the checked flag to be 1 and unchecked to 0. I can already do it myself (i.e. an ugly hack), but I don't see any native path with FormData.

+2
source share
2 answers

Currently, when creating a FormData object, a flag with the mark "on" is added, and the unchecked flag is not passed at all.

on used only if the value attribute is missing

I need to hack some hidden inputs in order to set checkboxes correctly

No. This is the correct flag handling. This is how they worked in forms, since the form element was added to HTML.

Check for the presence or absence of a flag in the code that processes it.

+3
source

Try it:

 var checkbox = $("#myForm").find("input[type=checkbox]"); $.each(checkbox, function(key, val) { formData.append($(val).attr('name'), this.is(':checked')) }); 

It always adds a field to FormData with a value of true when checked, or false when not checked.

+2
source

Source: https://habr.com/ru/post/1242246/


All Articles