[英]Select one Checkbox When All chekboxes are selected
我正在尋找邏輯,其中我使用php提取了while循環中的一些數據列表,如下所示。
if (mysql_num_rows($results) != 0) {
// displaying records.
while ($row = mysql_fetch_array($results)) {
echo '<div id="checkboxlist">';
echo '<tr>';
echo '<td><input name="checkbox[]" type="checkbox" class="checkbox1" value="'.$row['Id'].'" id="Checkbox1"></td>';
echo '<td>'.$row['first_name'].'</td>';
echo '<td>'.$row['last_name'].'</td>';
echo '<td>'.$row['phone'].'</th>';
echo '<td>'.$row['email'].'</td>';
echo '<td>'.$row['fax'].'</td>';
echo '<td><button><a href="edit.php?id='.$row['Id'].'">Edit</a></button></td>';
echo '<td><a onclick="return deleteRec();" href="ajax_delete.php?id='.$row['Id'].'" id="deleteOne">Delete</a></td>';
echo '<td><a href="view.php?id='.$row['Id'].'" target="_blank">View</a></td>';
echo '</div>';
}
} else {
echo '<td colspan="9"><h1>No contacts found.</td></h1>';
}
現在,如果要檢索多個數據,我們有多個復選框,我還有一個復選框,
<table>
<tr>
<td>
<input type="checkbox" name="checkAll" id="checkAll"/>
</td>
<td colspan="8" align="right">
<button type="submit" onClick="return massDelete()" name="delete" class="delete" disabled>Delete All</button>
</td>
</tr>
</table>
我感到困惑的是,如果我選擇了所有檢索到的數據復選框,則應該自動選擇“單獨”復選框,即,如果從檢索到的10個數據中選擇了5個數據,則不應選擇id="checkAll"
的復選框。 相反,如果我選擇所有10個復選框,則僅應選擇id="checkAll"
特定復選框。
聽着change
的事件checkboxes
,如果length
的checked
復選框與匹配length
上課的復選框checkbox1
然后check
的check all
復選框。
嘗試這個:
$('.checkbox1').on('change', function() { var bool = $('.checkbox1:checked').length === $('.checkbox1').length; $('#checkAll').prop('checked', bool); }); $('#checkAll').on('change', function() { $('.checkbox1').prop('checked', this.checked); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <input type="checkbox" name="checkAll" id="checkAll" />Check all <br> <input name="checkbox[]" type="checkbox" class="checkbox1" value=""> <br> <input name="checkbox[]" type="checkbox" class="checkbox1" value=""> <br> <input name="checkbox[]" type="checkbox" class="checkbox1" value=""> <br> <input name="checkbox[]" type="checkbox" class="checkbox1" value="">
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.