javascript - Active and inactive checkbox with JQuery -


i working on web app asp.net mvc , have problem html checkbox element.so viewmodel checkbox's:

public class serviceviewmodel:viewmodelbase {     public virtual string name { get; set; }     public virtual bool isactive { get; set; }  } 

my view this:

        @model ilist<saloon.viewmodels.serviceviewmodel>          <ul>              @for (var = 0; < model.count; i++)              {                 <li>                     <input type="checkbox" class="chk" name="@model.name[i]" id="@(i+1)" />                     <label for="@model.name[i]">@model.name[i]</label>                 </li>              }         </ul> 

and jquery code checked , unchecked checkbox's:

<script> $('input[type=checkbox].chk').click(function () {     if ($(this).is(":checked")) {         $(this).prop("checked", false);     } else {         $(this).prop('checked', true);     } }); </script> 

but jquery code not work.even i've tried $('input[type=checkbox].chk').change(function () {... , other ways available jquery , found on internet doesn't work too.

your code there, missed thing, document.ready()

your code supposed work, there's nothing wrong changing status of checkboxes.

instead, .click() won't initilized because jquery not loaded. maybe there's factor not firing events properly, can guess provided.

<script> $(document).ready(function() {     $('input[type=checkbox].chk').click(function () {         if ($(this).is(":checked")) {             $(this).prop("checked", false);         } else {             $(this).prop('checked', true);         }     }); }); </script> 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -