开发者

how to know weather checkbox checked or not with javascript

开发者 https://www.devze.com 2023-01-03 20:24 出处:网络
function checkAll(custId){ var id; if(document.getElementById(custId).checked == true){ <%for (CustomerGroup custGroup : customerGroupList) {%>
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 开发者_如何学JAVA: customerGroupList) {%>

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

Here I have passed custId as a id of checkbox.Problem here is if(document.getElementById(custId).checked == true

each time this condition will evaluate to false and thus control goes on else part. so how can i know wether checkbox is cheched or not ?.


The checked property is what you need to use to verify if a checkbox is 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>


<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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号