如何在 javascript 中获取 checkbox1.Checked 属性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18308520/
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-10-27 11:25:50  来源:igfitidea点击:

How to get checkbox1.Checked property within a javascript?

javascriptasp.net

提问by ARATHY

I have two checkboxes.when i checked both the checkboxes i wants to generate an alert "Passed". I have written like this

我有两个复选框。当我选中两个复选框时,我想生成一个警报“通过”。我是这样写的

<html xmlns="http://www.w3.org/1999/xhtml">
 <head runat="server">
<title>Untitled Page</title> 

  <script type="text/javascript" >
   function validate() {
    if (document.getElementById('CheckBox1').checked &&   document.getElementById('CheckBox2').checked) {

        alert("passed");

     } else {
        alert("Referred")
    }
}
</script>


   <asp:CheckBox ID="CheckBox1" runat="server"  OnCheckedChanged="javascript:validate();" />
   <asp:CheckBox ID="CheckBox2" runat="server"  OnCheckedChanged="javascript:validate();" />

but the problem is this (checkbox1).checked is not getting inbuildly within the .how can i get it? is ther any need for adding extra properties to my project for getting the '.checked' property?its first time i'am using javascript. and my visualstudio version is 3.5.please help

但问题是这个 (checkbox1).checked 并没有在 .how 中得到内置?是否需要向我的项目添加额外的属性以获取“.checked”属性?这是我第一次使用 javascript。我的visualstudio版本是3.5.请帮忙

回答by Abijeet Patro

OnCheckedChangedis a server side event, and you are handing it on the client side.

OnCheckedChanged是一个服务器端事件,你在客户端处理它。

<asp:CheckBox ID="CheckBox1" runat="server"  onchange="validate();" />
<asp:CheckBox ID="CheckBox2" runat="server"  onchange="validate();" />

Also you might need to use .ClientID-

你也可能需要使用.ClientID-

(document.getElementById('<%=CheckBox1.ClientID%>').checked &&   document.getElementById('<%=CheckBox2.ClientID%>').checked)

回答by mj930112

try this..

试试这个..

(in html)

(在 html 中)

<input type="checkbox" id="checkbox1" value="checkbox1">
<input type="checkbox" id="checkbox2" value="checkbox2">    

(in js)

(在js中)

if(($('#checkbox1').attr('checked'))&&($('#checkbox2').attr('checked')) ){
  alert("passed");
} else {
  alert("Referred");    
}