mardi 31 janvier 2017

Uniform and jQuery's Form.Serialize() ignores checked checkboxes

I'm using jQuery 3.1.1 (the latest version as of this post) and the Uniform plugin (just including in case this matters - likely doesn't) to make checkboxes look nice.

I'm encountering the following:

  1. Within a Form, I'm using MVC/Razor's CheckBoxFor to render a regular checkbox input and hidden additional input for the checkbox in addition to other inputs. This results in code like this:

    <input id="mycheckbox" name="mycheckbox" type="checkbox" value>

    <input name="mycheckbox" type="hidden" value>

  2. For test purposes, I'm checking the box on the UI.

  3. I verified that the checkbox input is now true (via $("#mycheckbox").prop("checked")) but the hidden input is false (same verification). I'm not sure if this is the culprit or not - in that case it may be a uniform issue.
  4. When then calling $form.serialize() on that form, both inputs (checkbox and hidden) are serialized to nothing, meaning &mycheckbox=&mycheckbox=

So, two issues:

  1. Somehow serialize() doesn't get that the regular checkbox input is checked
  2. There is double-serialization, but I suppose serialize() is just grabbing all input's so this is probably fine.

How can I solve #1?




Aucun commentaire:

Enregistrer un commentaire