我已经在这里学习所有的答案(https://stackoverflow.com/a/2191026),但建议由@davydepauw和@emeraldjava不工作...下面的代码不会选择,即使最清晰的代码/取消选择PHP代码中的框。JavaScript和PHP - 检查,并取消所有复选框
echo "<form action=$fileName method='post'>";
...
<script language='JavaScript'>
$('#select-all').click(function(event) {
if(this.checked) {
// Iterate each checkbox
$(':checkbox').each(function() {
this.checked = true;
});
}
else {
// Iterate each checkbox
$(':checkbox').each(function() {
this.checked = false;
});
}
});
</script>";
...
// This should select/deselect all checkboxes below:
echo "<input type='checkbox' name='select-all' id='select-all' />";
...
// The below is in the WHILE loop fetching data from MySQL:
echo "<input type='checkbox' name='IndustryID' value='" . $row['IndustryID'] . "'>";
...
</form>
对于@DavidThomas要求,下面是渲染代码:
<body>
<script language='JavaScript'>
$('#select-all').click(function(event) {
if(this.checked) {
// Iterate each checkbox
$(':checkbox').each(function() {
this.checked = true;
});
}
else {
// Iterate each checkbox
$(':checkbox').each(function() {
this.checked = false;
});
}
});
</script>
...
<form action=XXX.php method='post'>
...
<input type='checkbox' name='select-all' id='select-all' />
...
<input type='checkbox' name='IndustryID' value='3'>
...
<input type='checkbox' name='IndustryID' value='5'>
...
<input type='checkbox' name='IndustryID' value='148'>
...
</form>
</body>
你能张贴在jsfiddle.net一个精简的例子吗? – Arno 2012-04-18 15:02:34
你的PHP是无关的jQuery和Javascript一般,请出示呈现的HTML,而不是服务器端脚本。 – 2012-04-18 15:03:00
看看这里:http://api.jquery.com/live/,我觉得你没有得到的结果,因为元素创建你的JavaScript之前 – Gerep 2012-04-18 15:04:53