日期:2014-05-16  浏览次数:20406 次

两组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 o