简体   繁体   English

如何检查具有布尔值的复选框

[英]How to check a checkbox with boolean values

So the HTML spec says that you should add a checked attribute to the <input type="checkbox"> tag to mark it as checked. 因此,HTML规范要求您应将一个checked属性添加到<input type="checkbox">标记中,以将其标记为选中状态。 But I only have a boolean true or false value. 但是我只有一个布尔值truefalse

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 onload seem not to trigger: 按照设计,我无法在布尔值解析为html之前添加任何逻辑:(。是否可以仅使用html或js检查加载时具有布尔值的Checkbox?我尝试了以下操作,但是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. 然后,服务器会自动用数据库值替换%IsValide% var。 Here true or false. 这里是对还是错。 But then the checkbox is always checked, even if the value is false. 但是,即使该值为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. 我知道这是由于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.). 现在,我正在寻找一种解决方案,以使用%IsValide%的true或false值来使用javascript(或什至更好的是使用html)正确选中该框。 I can use the %IsValide% at any position in the html tag. 我可以在html标记的任何位置使用%IsValide% For example: 例如:

<input type="checkbox" onload="javascript:this.checked=%IsValide%" /> 

But I can not add logic before it get parsed to HTML like this: 但是我无法在将其解析为HTML之前添加逻辑,如下所示:

if(%IsValide%) {
 %IsValide%="checked";
}
else {
 %IsValide%="";
}

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: 或避免body的onload属性进行检查:

<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

If your company's application sets checked="true" or checked="false" as you can see, this will not matter since checked is 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)

jsFiddle example jsFiddle示例

I think ultimately you're going about this the wrong way, but if I had no other option like 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);

http://jsfiddle.net/mu81yz7y/ http://jsfiddle.net/mu81yz7y/

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. 但是,再次,这似乎是一个完整的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 asking that question. 如果您问“如何使用胶带将发动机固定到汽车车身上”,也许您应该停下来看看是否应该这个问题。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM