首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

两组checkbox,互相disable的有关问题

2012-02-03 
两组checkbox,互相disable的问题。东西很简单,但是现在着急要。我的js水平实在poor 需求:两组checkbo

两组checkbox,互相disable的问题。
东西很简单,但是现在着急要。我的js水平实在poor

> > > > 需求:
        两组checkbox,点击其中一组的一个,对应一组的一个变成disable状态;只有取消被选中的项后,才可以设置对应的一个checkbox。

        提示:两个对应的项的value是一样的,只能通过这个关联了。

> > > >   example:
组1:
<input   type= "checkbox "   name= "a_1 "   value= "value_1 "   onclick= "clickOption(this,   'a_1 ') ">   A1

<input   type= "checkbox "   name= "a_1 "   value= "value_2 "   onclick= "clickOption(this,   'a_1 ') ">   A2   <p>
组2
<input   type= "checkbox "   name= "b_1 "   value= "value_1 "   onclick= "clickOption(this,   'b_1 ') ">   B1

<input   type= "checkbox "   name= "b_1 "   value= "value_2 "   onclick= "clickOption(this,   'b_2 ') ">   B1

当点击A1的时候,B1将disable;
当取消A1的选中时,B1恢复。
注:A1和B1的value是一样的,可以通过value将两项关联。

[解决办法]
<form name= "f ">
<input type= "checkbox " name= "a_1 " value= "value_1 " onclick= "clickOption(this) "> A1
<input type= "checkbox " name= "a_1 " value= "value_2 " onclick= "clickOption(this) "> A2

<input type= "checkbox " name= "b_1 " value= "value_1 " onclick= "clickOption(this) "> B1
<input type= "checkbox " name= "b_1 " value= "value_2 " onclick= "clickOption(this) "> B2

<input type= "checkbox " name= "C_1 " value= "value_1 " onclick= "clickOption(this) "> C1
<input type= "checkbox " name= "C_1 " value= "value_2 " onclick= "clickOption(this) "> C2
</form>

<script>
function clickOption(obj)
{
frm = document.f;
for(c=0;c <frm.length;c++)
{
o = frm.elements[c];
if(o.type== "checkbox "&&o.name!=obj.name&&o.value==obj.value) o.disabled=obj.checked;
}
}
</script>


测试通过
[解决办法]
<input type= "checkbox " name= "a_1 " value= "value_1 " onclick= "clickOption(this) "> A1

<input type= "checkbox " name= "a_1 " value= "value_2 " onclick= "clickOption(this) "> A2 <p>
组2
<input type= "checkbox " name= "b_1 " value= "value_1 " onclick= "clickOption(this) "> B1

<input type= "checkbox " name= "b_1 " value= "value_2 " onclick= "clickOption(this) "> B1

组3
<input type= "checkbox " name= "c_1 " value= "value_1 " onclick= "clickOption(this) "> C1

<input type= "checkbox " name= "c_1 " value= "value_2 " onclick= "clickOption(this) "> C1
<script language=javascript>
function clickOption(obj)
{
var name1=obj.name
re=/(_\d+)/
re.test(name1);
index=RegExp.$1
var re=new RegExp(index+ "$ ")
var ifCheck=(obj.checked)?true:false
var objs=document.getElementsByTagName( "input ")


for(var i=0;i <objs.length;i++)
{
if(objs[i].type== "checkbox ")
{
if(re.test(objs[i].name))
{
if(objs[i].name!=name1)
{
objs[i].disabled=ifCheck
}
}
}
}
}
</script>
[解决办法]
function clickOption(obj,name)
{
o = document.getElementsByTagName( "input ");
for(c=0;c <o.length;c++)
{
if(o[c].type= "checkbox "){
if(o[c].value==obj.value){

if(o[c] != obj){
o[c].disabled=obj.checked;
}
}
}
}
}

热点排行
Bad Request.