我在文档上有检查按钮。检查多少个复选框
<input type="checkbox" id="CustmerRequested"/>
我想知道在文档中有多少个框被选中。
我试图做alert(document.all.CustmerRequested.checked.length);
但它说未定义。
我怎样才能知道有多少盒子被检查?
我在文档上有检查按钮。检查多少个复选框
<input type="checkbox" id="CustmerRequested"/>
我想知道在文档中有多少个框被选中。
我试图做alert(document.all.CustmerRequested.checked.length);
但它说未定义。
我怎样才能知道有多少盒子被检查?
如果你开始构建需要这种网站的浏览器端编程,我建议看看jQuery。看到这里的教程:http://docs.jquery.com/Tutorials
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
$("#someButton").click(function() {
var checkedBoxes = $("#yourForm input:checked");
alert(checkedBoxes.length + " checked.");
});
});
</script>
+1:你打败了我。 – Powerlord 2010-06-24 20:38:19
使用类似Prototype或JQuery的Javascript框架来查找需要检查的元素,例如,在原型:
var inputs = $$('input');
这回则数组可以绕在计算中被选中的复选框,像这样的输入数量:
for (var i=0; i<inputs.length; i++){
if (inputs[i].type == 'checkbox' && inputs[i].checked) {
numChecked++;
}
}
是的,你可以用JavaScript来做到这一点。不,你不需要jQuery。
这里有一种方法:
function howManyAreChecked()
{
one = document.getElementById("one").checked;
two = document.getElementById("two").checked;
three= document.getElementById("three").checked;
var checkCount = 0;
if (one == true)
{ checkCount = checkCount + 1 }
if (two == true)
{ checkCount = checkCount + 1 }
if (three == true)
{ checkCount = checkCount + 1 }
alert(checkCount);
}
上面的例子假设你已经在HTML中有3个复选框,与ID为“一”,“二”和“三”。该脚本首先存储“checked”属性的值,可以是TRUE或FALSE。然后脚本查看每个不同的变量,如果为TRUE,则递增计数器。
'document.all'?你需要找到一个在1997年以后编写的教程。这是一个IE4主题。 – Quentin 2010-06-24 21:22:16