Java 复选框全部选中/取消选中(快速帮助)
我想要一个使用jquery或javascript的check/uncheckall按钮。 我曾尝试使用net中可用的示例,但没有任何效果Java 复选框全部选中/取消选中(快速帮助),java,javascript,jquery,Java,Javascript,Jquery,我想要一个使用jquery或javascript的check/uncheckall按钮。 我曾尝试使用net中可用的示例,但没有任何效果 <li> <span class="checkbox unchecked"> <label for="check1"></label> <input type="checkbox" id="select" name="select"> </
<li>
<span class="checkbox unchecked">
<label for="check1"></label>
<input type="checkbox" id="select" name="select">
</span>
</li>
这就是我如何在包含一些信息的循环中放置所有复选框的方法。
我从另一个jsp页面调用了它,并将它放在另一个jsp页面的表单中。
我尝试了可能的示例,但无法使用这些代码
请帮忙
我尝试过这个jQuery:
<script language="javascript">
$(function(){
$("#select").click(function () {
$('#check1').attr('checked', this.checked);
});
// if all checkbox are selected, check the select checkbox
// and viceversa
$("#check1").click(function(){
if($("#check1").length == $("#check1:checked").length) {
$("#select").attr("checked", "checked");
} else {
$("#select").removeAttr("checked");
}
});
});
</script>
$(函数(){
$(“#选择”)。单击(函数(){
$('#check1').attr('checked',this.checked);
});
//如果选中了“所有”复选框,请选中“选择”复选框
//维切维萨呢
$(“#检查1”)。单击(函数(){
如果($(“#检查1”).length==$(“#检查1:检查”).length){
$(“#选择”).attr(“选中”、“选中”);
}否则{
$(“#选择”).removeAttr(“选中”);
}
});
});
我不允许更改此复选框的设计和css。
<a rel="env" href="#select_all">Select All</a>
<a rel="env" href="#select_none">Select None</a>
<ul id="env">
<input type="checkbox"> data
<input type="checkbox"> data
</ul>
<script>
$("A[href='#select_all']").click( function() {
$("#" + $(this).attr('rel') + " INPUT[type='checkbox']").attr('checked', true);
return false;
});
$("A[href='#select_none']").click( function() {
$("#" + $(this).attr('rel') + " INPUT[type='checkbox']").attr('checked', false);
return false;
});
</script>
数据
数据
$(“A[href=”#全选“])。单击(函数(){
$(“#”+$(this.attr('rel')+“INPUT[type='checkbox']”)attr('checked',true);
返回false;
});
$(“A[href=”#选择_none'])。单击(函数(){
$(“#”+$(this.attr('rel')+“INPUT[type='checkbox']”)。attr('checked',false);
返回false;
});
代码中存在的一些缺陷
1) 选择器错误。$(“#selectall”)。单击(函数(){
选择id为selectall的输入,但在您的情况下,没有id为selectall的元素
这应该可以
$("#select").click(function () { //this select the input with id select..which is selectall in your case..
2) $('#check1').attr('checked',this.checked);
这会找到ID为check1的复选框…,我猜在您的情况下它是多个的。。因此,由于多个元素不能具有相同的ID。请将其更改为class并调用
$('.check1').attr('checked', this.checked);
最终输出..无需更改代码的长度
$(function(){
$("#select").click(function () {
$('.check1').attr('checked', this.checked);
});
// if all checkbox are selected, check the selectall checkbox
// and viceversa
$(".check1").click(function(){
if($(".check1").length == $(".check1:checked").length) {
$("#select").attr("checked", "checked");
} else {
$("#select").removeAttr("checked");
}
});
});
您不能为页面上的多个元素提供相同的id。请尝试将这些
id
更改为class
:
我在这里做了一把小提琴:
使用的html:
我在这里写了一篇关于使用jQuery切换复选框的博客文章 我还注意到,您已将复选框命名为
#checked1
,您可能希望将其更改为一个类,而不是多个类
$(document).ready(function() {
/** Toggle child checkboxes based on the #selectall's checked state. */
$("#selectall").click(function() {
this.checked = !(this.checked == true);
var ischecked = !(this.checked == true);
/** Set the checkboxes. */
$("#grdWhatever input:checkbox").attr("checked", function() {
this.checked = ischecked;
});
});
});
如果您有一个主复选框
#选择
,控制页面上所有其他复选框的状态:
$('#select').on('click', function(){
if(this.checked) $('input:checkbox').not($(this)).attr('checked', true);
else $('input:checkbox').not($(this)).attr('checked', false);
});
我想你需要这个,而不是使用id,请使用类 请把脚本放在页面的开头部分
<script>
$(document).ready(function(){
$(".check").click(function(){
if($(".checkall:checked").length<=0) {
$(".checkall").prop('checked', true);
} else {
$(".checkall").removeAttr("checked");
}
});
});
</script>
$(文档).ready(函数(){
$(“.check”)。单击(函数(){
if($(“.checkall:checked”).length您的js和html代码之间似乎没有任何关系。我应该在哪里调用javascript..在调用页还是调用页..我正在处理两个页面。。
$('#select').on('click', function(){
if(this.checked) $('input:checkbox').not($(this)).attr('checked', true);
else $('input:checkbox').not($(this)).attr('checked', false);
});
<script>
$(document).ready(function(){
$(".check").click(function(){
if($(".checkall:checked").length<=0) {
$(".checkall").prop('checked', true);
} else {
$(".checkall").removeAttr("checked");
}
});
});
</script>
<ul>
<li>
<span class="checkbox unchecked">
<label for="check1"></label>
<input type="checkbox" id="select" name="selectall" class="checkall">
</span>
</li>
<li>
<span class="checkbox unchecked">
<label for="check1"></label>
<input type="checkbox" id="select" name="selectall" class="checkall">
</span>
</li>
<li>
<span class="checkbox unchecked">
<label for="check1"></label>
<input type="checkbox" id="select" name="selectall" class="checkall">
</span>
</li>
</ul>
<input type="button" name="test" value="test" class='check'/>