如何确定选中哪个复选框?
问题描述:
I have an HTML table of four columns: studentID
, first_name
, last_name
and 5 checkboxes with grades. How can I determine in PHP (or maybe using JQuery?) which check box(grade) was checked for each studentID
?
<tr>
<td>
<?php print $stud_row['student_id']; ?>
</td>
<td>
<?php print $stud_row['first_name']; ?>
</td>
<td>
<?php print $stud_row['last_name']; ?>
</td>
<td>
<input type="checkbox" name="id" value="a"> A
<input type="checkbox" name="id" value="b"> B
<input type="checkbox" name="id" value="c"> C
<input type="checkbox" name="id" value="d"> D
<input type="checkbox" name="id" value="f"> F
</td>
</tr>
答
Here is a fully functional example on how to see which grade is selected using jQuery:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
$(function() {
$('.grade').on('change',function(){
if($(this).is(':checked')){
$('#grades').append($(this).val());
}else{
$('#grades').html($('#grades').html().replace($(this).val(),''));
}
});
});
</script>
</head>
<body>
<input class="grade" type="checkbox" name="a" value="a"> A <br>
<input class="grade" type="checkbox" name="b" value="b"> B <br>
<input class="grade" type="checkbox" name="c" value="c"> C <br>
<input class="grade" type="checkbox" name="d" value="d"> D <br>
<input class="grade" type="checkbox" name="f" value="f"> F <br>
<div id="grades"></div>
</body>
</html>
Then if you need to see it on the server just use ajax or a form to pass it back.