Maybe this should help
$(document).ready(function() { $('input[type=checkbox]').click(function() { alert("val="+$(this).val()+",checked="+$(this).attr('checked')); }); });
Use input[type=checkbox] to select input type="checkbox" as the checkbox is checked and entered with type = "checkbox"
And add also the value attribute.
Jsfiddle on
UPDATE
<form method="post" action="#"> PHP<input type="checkbox" name="ch[]" value="PHP"><br/> JS<input type="checkbox" name="ch[]" value="JS"><br/> JAVA<input type="checkbox" name="ch[]" value="JAVA"><br/> PYTHON<input type="checkbox" name="ch[]" value="PYTHON"><br/> <input type="submit" name="submit"/> </form>
And php
if($_POST){ foreach($_POST['ch'] as $post => $value){ echo "$post -> $value <br/>"; } }
They all have the same name, as they are a common group of buttons. You know what has been checked and which did not match their values.
Another way is
if($_POST){ foreach($_POST as $post => $value){ echo "$post -> $value <br/>"; } } <form method="post" action="#"> PHP<input type="checkbox" name="PHP"><br/> JS<input type="checkbox" name="JS"><br/> JAVA<input type="checkbox" name="JAVA"><br/> PYTHON<input type="checkbox" name="PYTHON"><br/> <input type="submit" name="submit"/> </form>
But the previous one seems to add more dynamic and less tiring (I would believe).
As for the question @Capsule
alert("val="+$(this).val()+",checked="+$(this).attr('checked')); <input type="checkbox"> Check me.
returns the value on , I believe the default value for the checkbox.
And the second experiment also gives the value on .
I hope I'm not mistaken.
Santosh Linkha Apr 11 2018-11-11T00: 00Z
source share