Javascript 如何检查带有布尔值的复选框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26889337/
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 check a checkbox with boolean values
提问by Jan Hommes
So the HTML spec says that you should add a checkedattribute to the <input type="checkbox">tag to mark it as checked. But I only have a boolean trueor falsevalue.
因此 HTML 规范说您应该checked向<input type="checkbox">标签添加一个属性以将其标记为已选中。但我只有一个布尔值true或false值。
As by design, I can't add any logic to the boolean value before it get parsed to html :(.
Is it possible to check a Checkbox with a Boolean value on load just with html or js? I tried the following, but the onloadseem not to trigger:
按照设计,我无法在布尔值解析为 html 之前向其添加任何逻辑:(。是否可以仅使用 html 或 js 在加载时检查带有布尔值的复选框?我尝试了以下操作,但是onload似乎没有触发:
<input type="checkbox" onload="javascript:this.checked=true" />
I know that I can do something like this in jQuery:
我知道我可以在 jQuery 中做这样的事情:
$("[checked='false']").prop('checked',false)
I am looking for an elegant way to solve it. This must parse the whole DOM-Tree and would slow down my page.
我正在寻找一种优雅的方法来解决它。这必须解析整个 DOM-Tree 并且会减慢我的页面速度。
Edit (trying to explain):
编辑(试图解释):
In my company there exist a application that does the following by design:
在我的公司中,有一个应用程序按设计执行以下操作:
<input type="checkbox" checked="%IsValide%" >
The Server than automatically replaces the %IsValide%var with a database value. Here true or false. But then the checkbox is always checked, even if the value is false. I am aware that this is because of the design of html5: checked="true" and checked="false" will marke the checkbox always as checked.
服务器然后自动用%IsValide%数据库值替换var。这里真假。但是,即使值为 false,该复选框也始终处于选中状态。我知道这是因为 html5 的设计:checked="true" 和 checked="false" 将始终将复选框标记为已选中。
Now I'am searching for a solution to use the true or false value from %IsValide%to check the box correctly with javascript (or even better, with html.). I can use the %IsValide%at any position in the html tag. For example:
现在我正在寻找一种解决方案来使用 true 或 false 值%IsValide%来使用 javascript (或者甚至更好,使用 html。)正确检查框。我可以%IsValide%在 html 标签的任何位置使用。例如:
<input type="checkbox" onload="javascript:this.checked=%IsValide%" />
But I can notadd logic before it get parsed to HTML like this:
但是我无法在像这样解析为 HTML 之前添加逻辑:
if(%IsValide%) {
%IsValide%="checked";
}
else {
%IsValide%="";
}
回答by William Green
I'm trying to understand what your asking. I'm thinking that you could accomplish it with some simple javascript:
我试图理解你的要求。我想你可以用一些简单的 javascript 来完成它:
document.getElementById("yourCheckBoxId").checked = false; //this case it sets checked to false you can also do true
to check it on page load:
在页面加载时检查它:
<body onload='document.getElementById("yourCheckBoxId").checked = true;'>
or avoid the onload attribute for body for checking it do this:
或者避免使用 onload 属性来检查 body 这样做:
<input type="checkbox" id="yourCheckBoxId" checked>
Then you just change it with javascript.
然后你只需用javascript更改它。
My other guess is that your wanting to change the value of the checkbox? Are you? Post a comment below my answer saying that I didn't understand what you are asking. Hope this helps! Here is my reference source: http://www.w3schools.com/jsref/prop_checkbox_checked.asp
我的另一个猜测是您想更改复选框的值?你是?在我的回答下面发表评论说我不明白你在问什么。希望这可以帮助!这是我的参考来源:http: //www.w3schools.com/jsref/prop_checkbox_checked.asp
回答by j08691
If your company's application sets checked="true"or checked="false"as you can see, this will not matter since checkedis a boolean attribute and "the presence of this Boolean attribute indicates that the control is selected by default", and the checkbox will always be checked.
如果您公司的应用程序设置checked="true"或checked="false"如您所见,这将无关紧要,因为它checked是一个布尔属性,并且“此布尔属性的存在表明默认情况下选择了该控件”,并且该复选框将始终处于选中状态。
This should fix it:
这应该解决它:
$('input[checked="false"]').prop('checked',false)
回答by aquinas
I think ultimately you're going about this the wrong way, but if I had no other optionlike you're saying, I think the best you'll end up with (performance wise) would be to add a class to your input (true or false)
我认为最终你会以错误的方式解决这个问题,但是如果我没有像你说的那样的其他选择,我认为你最终会得到的最好的(性能方面)是在你的输入中添加一个类(对或错)
<input type="checkbox" class="tocheck-true"></input>
<input type="checkbox" class="tocheck-false"></input>
and then look for that class:
然后寻找那个类:
$("input.tocheck-true").prop("checked",true);
Because we're searching only for inputs that have that css class, this should perform very well.
因为我们只搜索具有该 css 类的输入,所以这应该表现得很好。
But, again, this seems like a complete hack. If you're asking "how can I use duct tape to secure the engine to the body of my car," maybe you should stop to see if you should even be askingthat question. :)
但是,再一次,这似乎是一个完整的黑客。如果您问“我如何使用胶带将发动机固定到我的汽车车身上”,也许您应该停下来看看是否应该问这个问题。:)

