我可以选中并取消选中所有复选框吗?

时间:2013-12-03 17:54:48

标签: javascript html checkbox

需要一些帮助,每个复选框应该只允许显示该ID。

如果选中所有复选框,则会显示所有ID。

有没有办法让它在所有复选框都未选中时显示所有ID?

function numbercheck()
{
var vis = (form1.one.checked) ? "block" : "none"; 
document.getElementById("one").style.display = vis;

var vis = (form1.two.checked) ? "block" : "none"; 
document.getElementById("two").style.display = vis;

var vis = (form1.three.checked) ? "block" : "none"; 
document.getElementById("three").style.display = vis;

}

<form id="form1" name="form1" method="post" action="">

<input type="checkbox" name="one" onclick="numbercheck();" tabindex="one" />
one<br/>
<input name="two" onclick="numbercheck();" type="checkbox" value="two" />
two<br/>
<input name="three" onclick="numbercheck();" type="checkbox" value="three" />
three
</form>

1 个答案:

答案 0 :(得分:0)

这可以清理并且更简单我确定,但这应该做你想要的。

function numbercheck()
{
var vis = (form1.one.checked) ? "block" : "none"; 
document.getElementById("one").style.display = vis;

var vis = (form1.two.checked) ? "block" : "none"; 
document.getElementById("two").style.display = vis;

var vis = (form1.three.checked) ? "block" : "none"; 
document.getElementById("three").style.display = vis;

if (!form1.one.checked && !form1.two.checked && !form1.three.checked)
{
    document.getElementById("one").style.display = "block";
    document.getElementById("two").style.display = "block";
    document.getElementById("three").style.display = "block";
}

}