javascript 如何确定一个 DOJO Checkbox 是否被选中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15830633/
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
How to determine whether a DOJO Checkbox is checked or not?
提问by Shreyos Adikari
<input id="test" type="checkbox" value="test" data-dojo-type="dijit.form.CheckBox">
How to check above dojo checkbox is checked or not in my javaScript function.
如何检查上面的 dojo 复选框是否在我的 javaScript 函数中被选中。
回答by g00glen00b
You can check this in various ways. You can use plain HTML/DOM/JavaScript and use something like:
您可以通过多种方式进行检查。您可以使用纯 HTML/DOM/JavaScript 并使用以下内容:
if (document.getElementById("test").checked) { ... }
or with Dojo:
或使用道场:
if (dojo.byId("test").checked) { ... }
This is what @Shreyos Adikari meant I think but you can also use the widget itself (which is doing the same thing behind the screens) with:
我认为这就是@Shreyos Adikari 的意思,但您也可以使用小部件本身(在屏幕后面做同样的事情):
if (dijit.byId("test").checked) { ... }
The difference between the first two methods and the last one, is that the first two use DOM nodes, while the last one uses the Dojo CheckBox widget/object which has a similar property. I personally recommend the last one, because this should alwayswork, even if they decide to change their template.
前两种方法与后一种方法的区别在于,前两种方法使用 DOM 节点,而后一种方法使用具有相似属性的 Dojo CheckBox 小部件/对象。我个人推荐最后一个,因为即使他们决定更改模板,这也应该始终有效。
But anyhow, there are plenty of examples on the web about how to achieve this (even on the Dojo documentation itself), I recommend you to view the API Documentationor at least the examples.
但无论如何,网络上有很多关于如何实现这一点的示例(甚至在 Dojo 文档本身),我建议您查看API 文档或至少查看示例。
回答by Shreyos Adikari
You can use javascript function checked over id, like:
您可以使用 javascript 函数检查 id,例如:
if (test.checked == 1){
alert("checked") ;
}
else{
alert("unchecked") ;
}
Here .checked will return "1" in case the checkbox is checked.
Please try this in your javascript and let me know in case of any concern.
如果复选框被选中,这里 .checked 将返回“1”。
请在您的 javascript 中尝试此操作,如有任何疑问,请告诉我。