在javascript中单击另一个时,选中/取消选中复选框

时间:2013-03-18 16:46:56

标签: checkbox

单击另一个复选框时,尝试清除2个复选框。我已经搜索过这个答案,并尝试使用示例中的信息,但出于某种原因,我尝试的无效。这是我目前正在使用的代码。

function control()
{
/*var one = document.getElementById('one');
var two = document.getElementById('two');
var three = document.getElementById('three');*/

if(document.forms[0].one.checked == true)
{
    document.forms[0].two.checked == false;
    document.forms[0].three.checked == false;
}

if(document.forms[0].two.checked == true)
{
    document.forms[0].three.checked == false;
    document.forms[0].one.checked == false;
}

if(document.forms[0].three.checked == true)
{
    document.forms[0].one.checked == false;
    docuemnt.forms[0].two.checked == false;
}
}
</SCRIPT>
</head>
<body>
<form action="" method="POST" name="getArea">
<input type="checkbox" onClick="control()" name="one">Standard Shipping<br />
<input type="checkbox" onClick="control()" name="two">Overnight Shipping<br />
<input type="checkbox" onClick="control()" name="three">Priority Shipping<br />
</form>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

你在尝试下面的东西吗?

<html>
<head>
 <script type="text/javascript">

function control(idSelected)
{
/*var one = document.getElementById('one');
var two = document.getElementById('two');
var three = document.getElementById('three');*/

if(idSelected==1)
{
    document.forms[0].two.checked = false;
    document.forms[0].three.checked = false;
}

if(idSelected==2)
{
    document.forms[0].three.checked = false;
    document.forms[0].one.checked = false;
}

if(idSelected==3)
{
    document.forms[0].one.checked = false;
    docuemnt.forms[0].two.checked = false;
}
}
</SCRIPT>
</head>
<body>
<form action="" method="POST" name="getArea">
<input type="checkbox" onClick="control(1)" name="one">Standard Shipping<br />
<input type="checkbox" onClick="control(2)" name="two">Overnight Shipping<br />
<input type="checkbox" onClick="control(3)" name="three">Priority Shipping<br />
</form>
</body>
</html>