欢迎各位兄弟 发布技术文章
这里的技术是共享的
下面是自己亲自做的
<input type="checkbox" onclick="setCheck(this)">
function setCheck(btn){
$(btn).parents('.bg-info').next('ul').find('input').prop('checked', btn.checked); }
Ask QuestionAsked Modified 8 years, 8 months agoViewed 47k times25
Fought with a bunch of examples and, being still new to jQuery/Javascript, cannot get my code to function (here my my template in gsp):
<table>
<thead>
<tr>
<th><input type="checkbox" name="selectAll" onclick="selectAll(this.checked);"/></th>
</tr>
</thead>
<tbody>
<td>
<td><input type="checkbox" name="domainList" value="${domainInstance.id}"/></td>
</tbody>
<table>
I have the following javascript snippet in my main gsp, that calls the template:
function selectAll(status) {
}
How do I select all checkboxes from the selectAll name?
Shareedited Mar 12, 2012 at 17:44Dónal185k asked Mar 12, 2012 at 14:30
user823021241,143
Add a comment 正确答案
Since you are using jQuery, you should use an onclick
handler like below for selectAll.
$(':checkbox[name=selectAll]').click (function () {
$(':checkbox[name=domainList]').prop('checked', this.checked);
});
Please note that the above code is going to look into the entire dom for the checkbox with name=selectAll
and set the status of the checkbox with name=domainList
.
Below is a slightly better version with minor markup change,
$('#selectAllDomainList').click(function() {
var checkedStatus = this.checked;
$('#domainTable tbody tr').find('td:first :checkbox').each(function() {
$(this).prop('checked', checkedStatus);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<table id="domainTable">
<!-- Added ID -->
<thead>
<tr>
<th>
<!-- Added ID to below select box -->
<input type="checkbox" name="selectAll" id="selectAllDomainList" />
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<input type="checkbox" name="domainList" value="${domainInstance.id}" />
</td>
</tr>
<tr>
<td>
<input type="checkbox" name="domainList" value="${domainInstance.id}" />
</td>
</tr>
<tr>
<td>
<input type="checkbox" name="domainList" value="${domainInstance.id}" />
</td>
</tr>
<tr>
<td>
<input type="checkbox" name="domainList" value="${domainInstance.id}" />
</td>
</tr>
</tbody>
<table>
Shareedited Oct 23, 2014 at 0:54answered Mar 12, 2012 at 14:34Selvakumar Arumugam79.2k
This method is the way to go - unobtrusive JavaScript. Ryan, if you use this method, be sure to take the onclick attribute out of your selectAll input. – Surreal Dreams Mar 12, 2012 at 14:39
$("input:checkbox").prop("checked", status);
Shareedited Jul 19, 2012 at 12:42Luke11.4k answered Mar 12, 2012 at 14:34
Ray21.5kAdd a comment2
to select all checkboxes with name = selectAll and set their status you can do
function selectAll(status) {
$('input[name=selectAll]').each(function(){
$(this).prop('checked', status);
});
}
Shareanswered Mar 12, 2012 at 14:34Nicola Peluchetti76.1kAdd a comment
来自 https://stackoverflow.com/questions/9669005/jquery-toggle-select-all-checkboxes/9669062#9669062
Possible duplicate of stackoverflow.com/questions/2228382/… ? – T. Junghans Mar 12, 2012 at 14:34
$(':input:checkbox[name="selectAll"]').prop('checked', true)
Bind it to the checkboxes change event and add some conditions – Johan Mar 12, 2012 at 14:35