vendredi 14 septembre 2018

How to add/remove item on the basis of checkbox checked or unchecked?

I have written below lines of code

                var selectedFinal = [];
                 //On click check all records
                $(document).on("click", "#completebatch,.commoncheckbox", function () { 

                    var checkBoxCount = 0;
                    var selected = [];

                    $("#selected_students").empty();

                    $('#studentListBody input:checked').not(".disabled-check").each(function () {
                        var id = $(this).val();
                        if ($("#" + id).is(":checked"))
                        {
                            checkBoxCount++;
                            selected.push(id);
                        } 
                        else
                        {
                            checkBoxCount--;
                            selected.pop(id);
                        }
                    });

                    $("#allStudentIds").val($.unique(selected));

               });

in HTML I have

 <table>
 <thead>
   <th><input onclick="checkenabledisable()" id="completebatch" type="checkbox"></th>
 </thead>

 <tbody>
<tr>
  <td><input class="commoncheckbox disabled-check"  value="578" type="checkbox" disabled=""></td>
 <td>abc</td>
</tr>
<tr>
  <td><input class="commoncheckbox" value="357" type="checkbox"></td>
  <td>abc</td>
</tr>
<tr>

  <td><input class="commoncheckbox" value="123" type="checkbox"></td>
  <td>abc</td>
</tr>
    </tbody>
  </table>

I have created html table with checkboxes, where user can uncheck or check the checkboxes. Based on checked or unchecked checkboxes I am trying to push the id's (value) of the checked checkboxes in the "selected" array. The above code is not adding the value of that checkbox. Also When I try to uncheck the checkbox that id is not removed from the "selected" array. Please help!!!




Aucun commentaire:

Enregistrer un commentaire