samedi 22 juillet 2017

Pass Checkbox checked value to Script Variable

I have a table with a checkbox column - when you click the checkbox it performs a script that includes an AJAX request. I'm trying to set the checked state into a variable so I can then include this as a parameter in the AJAX POST request but I'm not able to get this into a variable so far.

Here's my checkbox input field with the script that is simplified to just capture the checked state of the input in the checkedState variable:

$(document).ready(function() {
  $("input.select-item").click(function() {
    var productID = $(this).val();
    var checkedState = $("input.select-item").attr("checked");
    console.log(checkedState);
  });
});
<script src="http://ift.tt/1v4HDwg"></script>

<td id="AT36288"><input type="checkbox" class="select-item checkbox" name="select-item" value="AT36288" /></td>

So if the checkbox was not checked and the user checks this I would like to capture the value of "checked" in the variable, and if the checkbox was checked and the user unchecked this I would like to capture the value of "uncheck" in the variable. I'm getting an undefined result for the variable in the console.




Aucun commentaire:

Enregistrer un commentaire