0
我有复选框,其值从数据库填充。我可以选择并取消选中所有复选框,但我无法获取单个复选框的值。这是我迄今所做的, 的复选框,从数据库填充:从复选框中获取值
<form>
<br>
<input type="checkbox" id="All"> <b>Select/DeSelect All</b><br>
<?php
$sql = 'SELECT Subject_Category from {extdb_subjects} where level=1 ';
$res = db_query($sql);
foreach ($res as $row)
{
echo "<input type='checkbox' id='sub_names' name='{$row->Subject_Category}'
value='{$row->Subject_Category}'";
echo ">{$row->Subject_Category}";
echo "<br>";
}
?>
</form>
jQuery脚本选择去选择选中的复选框的所有复选框,个别显示:
jQuery(function(){
jQuery('#All').change(function() {
var checkboxes = jQuery(this).closest('form').find(':checkbox');
if(jQuery(this).is(':checked')) {
checkboxes.attr('checked', 'checked');
} else {
checkboxes.removeAttr('checked');
}
});
jQuery('#sub_names').change(function() { //This is not able to get the values
if(jQuery(this).is(':checked')) {
alert(jQuery("input[type=checkbox]:checked").val());
}
});
});
任何建议将不胜感激。
这可能有助于http://stackoverflow.com/questions/786142/how-to-retrieve-checkboxes-values-in-jquery – noob 2013-03-02 09:17:05
你的问题是你有很多元素具有相同的ID(我认为),所以只有第一个会有你的onChange处理程序。 – 2013-03-02 09:18:52
@RC是的,因为只有第一个元素值显示时点击复选框,其余的没有效果。 – 2013-03-02 09:26:51