Javascript 如何使用javascript知道天气复选框是否选中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3074979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:11:59  来源:igfitidea点击:

how to know weather checkbox checked or not with javascript

javascript

提问by chetan

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;
            <%}%>
    }
    <%}%>
}
}

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

这里我传递了 custId 作为复选框的 id。这里的问题是 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 ?.

每次此条件将评估为假,因此控制继续进行其他部分。那么我怎么知道复选框是否被检查?

回答by Darin Dimitrov

The checkedproperty is what you need to use to verify if a checkbox is checked:

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>

回答by deepak

<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

代码来自 www.neonbrains.com