我有HTML頁面,它有多個複選框,並且可以單獨檢查它們。我有按鈕select
,所以我想要做的是。當我點擊選擇所有的複選框應該被選中,並取消選擇。當我點擊使用jQuery的選擇按鈕時,如何選擇所有複選框
的Html
<html>
<head>
<script src="jquery.js"></script>
</head>
<body>
<script>
$(document).ready(function() {
$('#selectAll').click(function(event) { //on click
if(this.checked) { // check select status
$('.btn-chk').each(function() { //loop through each checkbox
this.checked = true; //select all checkboxes with class "checkbox1"
});
}else{
$('.btn-chk').each(function() { //loop through each checkbox
this.checked = false; //deselect all checkboxes with class "checkbox1"
});
}
});
});
</script>
<table id="example" cellspacing="0" width="20%">
<thead>
<tr>
\t <th><button type="button" id="selectAll" class="myClass">Select</button></th>
\t \t <th>number</th>
<th>company</th>
\t \t <th> Type</th>
\t \t <th> Address</th>
\t \t <th>Code</th>
\t </tr> \t \t \t \t \t \t \t \t \t
</thead>
\t \t \t
<tbody> \t \t \t \t \t \t \t \t \t \t
<tr> \t \t \t \t \t \t \t \t \t \t \t
<td><span class="button-checkbox">
<button type="button" class="btn-chk" data-color="success"></button>
<input type="checkbox" class="hidden" />
</span>
\t \t </td>
\t \t <td>1</td>
\t \t <td>APPLE</td>
\t \t <td>IT</td>
\t \t <td>San Jones</td>
\t <td>US</td>
\t \t </tr>
\t \t <tr>
\t \t <td><span class="button-checkbox">
\t \t <button type="button" class="btn-chk" data-color="success"></button>
\t <input type="checkbox" class="hidden" />
\t \t </span>
</td>
<td>2</td>
\t \t <td>DELL</td>
\t \t <td>IT</td>
<td>San Jones</td>
<td>US</td>
</tr>
<tr>
<td><span class="button-checkbox">
\t <button type="button" class="btn-chk" data-color="success"></button>
\t <input type="checkbox" class="hidden" />
</span>
\t </td>
\t <td>3</td>
\t <td>Amazon</td>
\t <td>IT</td>
\t <td>San Jones</td>
\t <td>US</td>
\t </tr>
\t <tr>
\t <td><span class="button-checkbox">
<button type="button" class="btn-chk" data-color="success"></button>
<input type="checkbox" class="hidden" />
</span>
\t </td>
\t <td>4</td>
\t <td>Microsoft</td>
\t <td>IT</td>
\t <td>San Jones</td>
\t <td>US</td>
\t </tr>
\t </tbody>
\t \t \t \t \t \t \t \t \t
</body>
</html> \t \t \t \t \t \t \t \t \t \t \t
\t \t \t \t \t \t \t \t \t \t \t
輸出
在我有選擇按鈕的圖像。我要的是當我點擊選擇按鈕所有複選框應該得到選擇
我不明白,你是否檢查選擇按鈕是否被選中,並使用結果來決定是否選擇全部或不選。一個按鈕不能被檢查? –
你是否在這種功能之後:http://jsfiddle.net/Puddster/k9ryt981/?當你點擊頂部按鈕時,下面的所有內容都會被檢查? – Pudd
您並未遍歷每個複選框。將.btn-chk更改爲.hidden以循環遍歷所有複選框。 – Zee