![](/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.