lundi 26 mars 2018

get array of checked and unchecked checkboxes

I have checkbox input fields that are added to DOM dynamically. I want to get all checked and unchecked values as mentioned here.

HTML:

<input type="checkbox" name="lead_check[]" value="1">

Javascript:

$('#includes').click(function(e)
{
  e.preventDefault();
  $('#include-fields').append('<div class="holder-includes"><a href="#" class="remove-includes"><i class="fa fa-trash-o pull-right"></i></a>' +
    '<select name="team[]" class="form-control team">'+
    '<input type="checkbox" name="lead_check[]" value="1"></div>');

  var items = "";
  $.post("teamFetch", function(data)
  {
    $.each(data,function(index,item) 
    {
      items+="<option value='"+item.id+"'>"+item.title+"</option>";
    });

    $(".team:last").html(items); 
  }, "json");



});

I am getting only checked values in lead_check[] array.

[lead_check] => Array ( [0] => 1 [1] => 1 [2] => 1 ).

I need to get all unchecked values too.

[lead_check] => Array ( [0] => 1 [1] => 0 [2] => 1 [3] => 0).

I tried adding hidden input fields too. But they didn't help either.

How do I achieve desired output in case of dynamically generated checkbox input fields.

Any help is very much appreciated. Thanks.




Aucun commentaire:

Enregistrer un commentaire