in jquery (using form.serialize ()), I get tw...">

Html.CheckBox displays two checkboxes

When I try to serialize <%: Html.CheckBox("Something", true) %> in jquery (using form.serialize ()), I get two checkboxes, one says true and the other says false. I know that MVC displays true, false for checkboxes that are true, so if I don't go through javascript, I will just check for true, but how do I know if my checkbox is checked after the form. serialization?

+4
source share
2 answers

If you look at the HTML output, you will see that Html.Checkbox () actually displays additional hidden input there with the same name. That is why you see two meanings. I believe that MS decided to do this so that the correct bool gets POSTED (and not the string value 'on' for flags).

To get around this, I no longer use the Html.Checkbox () helper, I always just write the full HTML.

+4
source

Why not just:

 var isChecked = $('#idofyourcheckbox').is(':checked'); 

As for the values ​​stored in the form.serialize() , they are intended to go to the server, where you can simply work with Boolean properties.

0
source

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


All Articles