我不是jQuery的专家,但我试图为我的应用程序创建一个小脚本.我想检查所有复选框,但它无法正常工作.
首先我尝试使用attr,之后我尝试了,prop但我做错了.
我先试了一下:
$("#checkAll").change(function(){
if (! $('input:checkbox').is('checked')) {
$('input:checkbox').attr('checked','checked');
} else {
$('input:checkbox').removeAttr('checked');
}
});
Run Code Online (Sandbox Code Playgroud)
但这没效果.
下一步:这比上面的代码效果更好
$("#checkAll").change(function(){
if (! $('input:checkbox').is('checked')) {
$('input:checkbox').prop('checked',true);
} else {
$('input:checkbox').prop('checked', false);
}
});
Run Code Online (Sandbox Code Playgroud)
这两个例子都不起作用.我试着谷歌搜索答案,但没有成功.
jsFiddle:http://jsfiddle.net/hhZfu/4/
谢谢
我有以下HTML代码:
<input type="checkbox" id="ckbCheckAll" />
<p id="checkBoxes">
<input type="checkbox" class="checkBoxClass" id="Checkbox1" />
<br />
<input type="checkbox" class="checkBoxClass" id="Checkbox2" />
<br />
<input type="checkbox" class="checkBoxClass" id="Checkbox3" />
<br />
<input type="checkbox" class="checkBoxClass" id="Checkbox4" />
<br />
<input type="checkbox" class="checkBoxClass" id="Checkbox5" />
<br />
</p>
Run Code Online (Sandbox Code Playgroud)
当用户检查时,ckbCheckAll必须选中所有复选框.我还有以下jquery代码:
$(document).ready(function () {
$("#ckbCheckAll").click(function () {
$(".checkBoxClass").attr('checked', this.checked);
});
});
Run Code Online (Sandbox Code Playgroud)
当我在浏览器中看到我的页面时,我得到以下结果:在第一次单击ckbCheckAll所有复选框时检查(这是正确的).在第二次单击ckbCheckAll所有复选框时未选中(这是正确的).但是在第3次尝试中没有发生任 也是在第四次尝试中没有发生任何事情等等.
问题出在哪儿?
我找到以下jquery/checkbox行为的原因时遇到问题.
$( this.obj + ' table.sgrid-content > thead > tr > th > input.select_all' ).on( 'click' , {grid:this} , function(event){
var grid = event.data.grid;
if( $(this).is(':checked') ){
$( grid.obj + ' table.sgrid-content > tbody > tr > td > input.select ' ).attr('checked','checked');
$( grid.obj + ' .sgrid-content > tbody > tr > td > input.select ' ).parents('tr').addClass('ui-state-highlight');
} else {
$( grid.obj + ' table.sgrid-content > tbody > tr > td > input.select ' ).removeAttr('checked');
$( grid.obj + ' table.sgrid-content …Run Code Online (Sandbox Code Playgroud) 与一堆示例进行了斗争,并且仍然是jQuery/Javascript的新手,无法让我的代码运行(这里是我在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>
Run Code Online (Sandbox Code Playgroud)
我在我的主gsp中有以下javascript片段,它调用模板:
function selectAll(status) {
}
Run Code Online (Sandbox Code Playgroud)
如何从selectAll名称中选择所有复选框?