【发布时间】:2011-10-07 13:16:16
【问题描述】:
我想根据单击的复选框使用 jquery 来执行某些操作。出于某种原因,甚至没有为下面的任何这些复选框触发 onClick。
<HTML>
<HEAD>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<SCRIPT language="javascript">
$(document).ready(function() {
$("#group1").click(function() {
$('.group1').attr('checked', this.checked);
});
});
function checkGroup (id ) {
// ...
// do anything you want here
alert ("id: "+id+" "name: " + id.name);
//...
}
</script>
<script type="text/javascript">
</script>
<TITLE>Multiple Checkbox Select/Deselect - DEMO</TITLE>
</HEAD>
<BODY>
<a href="">Link</a>
<H2>Multiple Checkbox Select/Deselect - DEMO</H2>
<table border="1">
<tr>
<th><input type="checkbox" id="group1" name="master" onClick="javascript:checkGroup('group1');"/></th>
<th>Cell phone</th>
<th>Rating</th>
</tr>
<tr>
<th><input type="checkbox" id="group2" name="master" onClick="javascript:checkGroup('group2');"/></th>
<th>Cell phone</th>
<th>Rating</th>
</tr>
</table>
</BODY>
编辑
好的,我已经更新了它,如下所示。它适用于第一组复选框,但不适用于第二组。
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<SCRIPT language="javascript">
$(document).ready(function() {
$("#masterCheckBox").click(function() {
$('.' + $(this).attr('class')).attr('checked', this.checked);
});
});
</script>
<title>Multiple Checkbox Select/Deselect - DEMO</title>
</head>
<body>
<a href="">Link</a>
<H2>Multiple Checkbox Select/Deselect - DEMO</H2>
<table border="1">
<tr>
<th><input type="checkbox" id="masterCheckBox" name="master" class="master1"/></th>
<th>Master</th>
<th>Rating</th>
</tr>
<tr>
<th><input type="checkbox" id="child1" class="master1" name="child"/></th>
<th>Child</th>
<th>Rating</th>
</tr>
<tr>
<th><input type="checkbox" id="child1" class="master1" name="child"/></th>
<th>Child</th>
<th>Rating</th>
</tr>
</table>
<table border="1">
<tr>
<th><input type="checkbox" id="masterCheckBox" name="master" class="master2"/></th>
<th>Master</th>
<th>Rating</th>
</tr>
<tr>
<th><input type="checkbox" id="child1" class="master2" name="child"/></th>
<th>Child</th>
<th>Rating</th>
</tr>
<tr>
<th><input type="checkbox" id="child1" class="master2" name="child"/></th>
<th>Child</th>
<th>Rating</th>
</tr>
</table>
</BODY>
</HTML>
编辑
好的,这是另一个更新。它现在适用于使用类元素的不同复选框组。 if 语句仅在复选框是主复选框时触发事件。
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<SCRIPT language="javascript">
$(document).ready(function() {
$('input[type=checkbox]').click(function() {
if($(this).attr('name')=='master'){
$('.' + $(this).attr('class')).attr('checked', this.checked);
}
});
});
</script>
<title>Multiple Checkbox Select/Deselect - DEMO</title>
</head>
<body>
<a href="">Link</a>
<H2>Multiple Checkbox Select/Deselect - DEMO</H2>
<table border="1">
<tr>
<th><input type="checkbox" id="masterCheckBox" name="master" class="master1"/></th>
<th>Master</th>
<th>Rating</th>
</tr>
<tr>
<th><input type="checkbox" id="child1" class="master1" name="child"/></th>
<th>Child</th>
<th>Rating</th>
</tr>
<tr>
<th><input type="checkbox" id="child1" class="master1" name="child"/></th>
<th>Child</th>
<th>Rating</th>
</tr>
</table>
<table border="1">
<tr>
<th><input type="checkbox" id="masterCheckBox" name="master" class="master2"/></th>
<th>Master</th>
<th>Rating</th>
</tr>
<tr>
<th><input type="checkbox" id="child1" class="master2" name="child"/></th>
<th>Child</th>
<th>Rating</th>
</tr>
<tr>
<th><input type="checkbox" id="child1" class="master2" name="child"/></th>
<th>Child</th>
<th>Rating</th>
</tr>
</table>
</BODY>
</HTML>
现在我需要更改它,以便如果未选中特定组的子复选框,我需要检查是否仅在选中至少一个子复选框时才选中主复选框。
编辑
好的,这是我想要实现的工作版本的另一个更新。
- 当主复选框被点击时,它的所有子复选框都被选中
- 当任何子复选框被点击时,其对应的主复选框也会被选中
- 当一个子节点未选中时,如果同一组中没有其他已选中的子节点,则其对应的主节点将被取消选中。
请就如何改进这一点提出任何提示/提示。
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<SCRIPT language="javascript">
$(document).ready(function() {
$('input[type=checkbox]').click(function() {
if($(this).attr('name')=='master'){
$('.' + $(this).attr('class')).attr('checked', this.checked);
}
if($(this).attr('name')=='child'){
var childChecked = false;
$('.' + $(this).attr('class')).each(function(){
if (this.checked && this.name=="child"){
childChecked=true;
}
});
if (childChecked==false){
$('.' + $(this).attr('class')).each(function(){
if (this.name=='master'){
this.checked = childChecked;
}
});
}else{
$('.' + $(this).attr('class')).each(function(){
if (this.name=='master'){
this.checked = childChecked;
}
});
}
}
});
});
</script>
<title>Multiple Checkbox Select/Deselect - DEMO</title>
</head>
<body>
<H2>Multiple Checkbox Select/Deselect - DEMO</H2>
<table border="1">
<tr>
<th><input type="checkbox" id="masterCheckBox" name="master" class="master1"/></th>
<th>Master</th>
<th>Rating</th>
</tr>
<tr>
<th><input type="checkbox" id="child1" class="master1" name="child"/></th>
<th>Child</th>
<th>Rating</th>
</tr>
<tr>
<th><input type="checkbox" id="child1" class="master1" name="child"/></th>
<th>Child</th>
<th>Rating</th>
</tr>
</table>
<table border="1">
<tr>
<th><input type="checkbox" id="masterCheckBox" name="master" class="master2"/></th>
<th>Master</th>
<th>Rating</th>
</tr>
<tr>
<th><input type="checkbox" id="child1" class="master2" name="child"/></th>
<th>Child</th>
<th>Rating</th>
</tr>
<tr>
<th><input type="checkbox" id="child1" class="master2" name="child"/></th>
<th>Child</th>
<th>Rating</th>
</tr>
</table>
</BODY>
</HTML>
【问题讨论】:
-
刚看到更新。这是您对
id的使用。 id 必须是唯一的。使用类代替 id:<input ... class="masterCheckBox" ... />和$(".masterCheckBox")
标签: javascript jquery checkbox