如何通过javascript知道天气复选框是否已选中

时间:2010-06-19 09:09:28

标签: javascript

function checkAll(custId){
        var id;
        if(document.getElementById(custId).checked == true){
        <%for (CustomerGroup custGroup : customerGroupList) {%>

            if(custId==<%=custGroup.getCustomerGroupId()%>){
                <%List<Account> list = accountDelegate
                    .findAccountEntityByGroupId(custGroup
                            .getCustomerGroupId());
            for (Account account : list) {%>
                        id=<%=account.getAccountId()%>
                        document.getElementById(id).checked=true;
                <%}%>
            }
    <%}%>
    }else {
        <%for (CustomerGroup custGroup : customerGroupList) {%>

            if(custId==<%=custGroup.getCustomerGroupId()%>){
            <%List<Account> list2 = accountDelegate
                    .findAccountEntityByGroupId(custGroup
                            .getCustomerGroupId());
            for (Account account : list2) {%>
                        id=<%=account.getAccountId()%>
                        document.getElementById(id).checked=false;
            <%}%>
    }
    <%}%>
}
}

这里我已将custId作为checkbox的id传递。这里的问题是if(document.getElementById(custId).checked == true

每次这个条件评估为假,因此控制继续其他部分。 所以我怎么知道是否有人选择了复选框?。

2 个答案:

答案 0 :(得分:3)

您需要使用checked属性来验证是否选中了复选框:

<html>
<head>
    <title>Test</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script type="text/javascript">
    window.onload = function() {
        if (document.getElementById('test').checked) {
            alert('the checkbox is checked');
        }
    };  
    </script>
</head>
<body>

<input id="test" name="test" type="checkbox" checked="checked" />

</body>
</html>

答案 1 :(得分:2)

<script language="javascript"><br>
 var test=document.getElementById('test');<br>

 if (test.checked == 1){<br>
          alert("yes,Checked") ;<br>
 }<br>
else<br>
{
<br>
 alert("No,Please Check.")<br>
 }<br>
 </script>

code by www.neonbrains.com