<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>
<title>jquery在属性中传变量</title>
<scripttype="text/javascript"src="jquery-1.3.1.js"></script>
<scripttype="text/javascript">
$(document).ready(function(){
varzc="zi".length;//zi在类属性class标识是子复选框复到字符串的长度和标识的fu长度是一样的在这里共用一个
$(":checkbox").click(function(){//单击复选框触发事件函数
varcheckbox=$(this);
varname=$(this).attr("class");
varb1=name.substring(zc,name.length);//得到所单击的这个复选框后缀的数字组合
varbb=$(this).attr("checked");//判断选中或未选中trueorfalse
varb="fu"+b1;//父复选框class的组合
vara="zi"+b1;//子复选框class的组合
varb22=$('tdinput[class="'+b+'"]');//链接字符得到父复选框此用的是属性选择器
if(bb==true){
$(b22[0]).attr("checked",true);//当被选中的时候父的复选框也会选中
}elseif(bb==false){
varbox=$('tdinput[class="'+a+'"]:checked');//取消选中会判断还有被选中的个数等于零时父的复选框取消选中大于零时选中
varcd=box.length;
if(cd==0){
$(b22[0]).attr("checked",false);
}elseif(cd>0){
$(b22[0]).attr("checked",true);
}
}
});
});
</script>.
</head>
<body>
<tablewidth="200"border="0"cellspacing="0"cellpadding="0">
<tr>
<tdscope="col">父选框<inputtype="checkbox"class="fu1"/> </td>
</tr>
<tr>
<td>苹果<inputtype="checkbox"class="zi1"/></td>
</tr>
<tr>
<td>香蕉<inputtype="checkbox"class="zi1"/></td>
</tr>
<tr>
<td>桔子<inputtype="checkbox"class="zi1"></td>
</tr>
<tr>
<tdscope="col">父选框<inputtype="checkbox"class="fu2"/> </td>
</tr>
<tr>
<td>苹果<inputtype="checkbox"class="zi2"/></td>
</tr>
<tr>
<td>香蕉<inputtype="checkbox"class="zi2"/></td>
</tr>
<tr>
<td>桔子<inputtype="checkbox"class="zi2"></td>
</tr>
</table>
</body>
</html>
此种方式效率比较高一些
还有一种方式如下:而下用的是循环遍历进行比较的,选项比较多的话,会影响到代码的执行效率。这里用到了自定义属性的方法
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>
<title>无标题文档</title>
<scripttype="text/javascript"src="jquery-1.3.1.js"></script>
<scripttype="text/javascript">
$(function(){
$('[type=checkbox]').click(function(){
$('.fu').each(function(){
varsign=$(this).attr('sign');
varflag=true;
$('.zi'+sign).each(function(){
//alert($(this).attr('names'));
if($(this).attr("checked")){
//alert('aaa');
//alert("dddd"+sign);
flag=false;
}
});
if(flag){
//alert($(this).attr('checked'));
$(this).attr('checked',false);
//alert("dddd"+sign);
flag=true;
}else{
$(this).attr('checked',true);
flag=true;
}
});});
});
</script>.
</head>
<body>
<tablewidth="200"border="0"cellspacing="0"cellpadding="0">
<tr>
<tdscope="col">父选框<inputtype="checkbox"class="fu"sign="1"/> </td>
</tr>
<tr>
<td>苹果<inputtype="checkbox"class="zi1"names="苹果"/></td>
</tr>
<tr>
<td>香蕉<inputtype="checkbox"class="zi1"names="香蕉"/></td>
</tr>
<tr>
<td>桔子<inputtype="checkbox"class="zi1"names="桔子"/></td>
</tr>
<tr>
<tdscope="col">父选框<inputtype="checkbox"class="fu"sign="2"/> </td>
</tr>
<tr>
<td>苹果<inputtype="checkbox"class="zi2"names="苹果"/></td>
</tr>
<tr>
<td>香蕉<inputtype="checkbox"class="zi2"names="香蕉"/></td>
</tr>
<tr>
<td>桔子<inputtype="checkbox"class="zi2"names="桔子"></td>
</tr>
</table>
</body>
</html>