![](/img/trans.png)
[英]Check/Uncheck all checkboxes with Javascript in a PHP array with keys
[英]Javascript and PHP - Check and Uncheck All Checkboxes
我已经在这里研究了所有答案( https://stackoverflow.com/a/2191026 ),但是即使@davydepauw和@emeraldjava建议的最清晰的代码也无法使用...下面的代码不会选择/取消选择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>
您必须将所有内容放入这样的document.ready事件中,否则当不存在元素且没有要附加的元素并使用正确的脚本标记时,将运行代码
<script type="text/javascript">
$(function(){
$('#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>
那是因为您在jquery代码之后添加了复选框。
将您的JavaScript代码更改为此
<script language='JavaScript'>
$(document).ready(function(){
$('#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>";
或在显示复选框后添加您的JavaScript
<script type="text/javascript">
$(function(){
$('#select-all').click(function(event) {
$(':checkbox').each(function() {
$(this).attr('checked', !checkbox.attr('checked'));
});
});
})
</script>
根本没有经过测试...只是在我脑海中
var selectAll = false;
if(selectAll) {
$('input:checkbox').removeAttr('checked');
selectAll = false;
}
else {
$('input:checkbox').attr('checked', 'checked');
selectAll = true;
}
您不能使用此..
$(function(){
// add multiple select / deselect functionality
$("#selectall").click(function () {
$('.case').attr('checked', this.checked);
});
// if all checkbox are selected, check the selectall checkbox
// and viceversa
$(".case").click(function(){
if(!$(".case:not(:checked)").length)
{
$("#selectall").attr("checked", "checked");
} else {
$("#selectall").removeAttr("checked");
}
});
});
这就是我构造PHP的方式。
<input type='checkbox' id='selectall'/>
$select = mysql_query ("SELECT * FROM tblname") OR DIE (mysql_error());
while ($row3=mysql_fetch_array($select_orders2)){
$idd = $row3['id'];
<input type='checkbox' class='case' name='checkbox[]' value='".$idd."'>
}
在此代码中。 sql检索的所有数据都将以类的情况进行迭代。 希望这会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.