vendredi 5 mai 2017

post certain value for each checked and unchecked checkbox

I have a group of checkbox controls that I want to post a certain value for the checked one and null for the unchecked ones in an array

I wrote the code snippet below but i always get the same value for all regardless their checked state

$("#bt1").click(function() {
  var checked = []
  var val = ''
  var $chbx = $("input[name='advsrc']")
  $chbx.each(function() {
    if ($chbx.is(':checked')) {
      val = 'ahmad'
    } else {
      val = 'null'
    }
    checked.push(val);
  });
  console.log(checked)

  return false;
})
<script src="http://ift.tt/1oMJErh"></script>
<input id="bt1" type="button" value="button" />
<input id="grndsrc" type="checkbox" checked="checked" name="advsrc" />
<input id="fnamesrc" type="checkbox" name="advsrc" />
<input id="lnamesrc" type="checkbox" name="advsrc" />
<input id="dobsrc" type="checkbox" name="advsrc" />
<input id="telsrc" type="checkbox" name="advsrc" />
<input id="ssnsrc" type="checkbox" name="advsrc" />



Aucun commentaire:

Enregistrer un commentaire