samedi 8 mai 2021

How to remove hidden field associated with checkbox in javascript?

I am using ASP.net MVC. I am posting multiple checkboxes as array to controller using name attribute.

   <input type="checkbox" name="checkboxes[]"id="@model.count">                                                                                                              
   <label for="@("model.count)"></label>                                                                                                                
  <input type="hidden" name="checkboxes[]" value="0" />

I want to send value "on" when checkbox is checked and 0 when not checked. So in javascript, I have done below code.

        $('input[type=checkbox]:not(:checked)').each(function () {
        // set value 0 
        $(this).attr('checked', true).val(0);
     });

    $('input[type="checkbox"]:checked').each(function () {          
        $(this).next('input["type=hidden"]').remove();             
    });

But it's not working. Hidden field is not getting removed for checked checkboxes. I want to send array whose length is equal to no of checkboxes only.(and not hidden field). How should I do that?




Aucun commentaire:

Enregistrer un commentaire