Javascript 输入复选框 true 或选中或 yes

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

input checkbox true or checked or yes

javascripthtmlcheckboxhtml-input

提问by user1032531

I've seen the three implementations of pre-selecting a checkbox. I started off using checked="checked" because I thought it was the "proper" way (never did like the "checked"="yes" however). I am thinking of changing to checked="true" as it seems more readable and is easier to code the JavaScript. Note that this same question applies to other attributes such as "disabled"="disabled" versus "disabled"="true". As long as I am consistent, is using "true" the preferred approach? Thank you

我已经看到了预选复选框的三种实现。我开始使用checked="checked",因为我认为这是“正确的”方式(但是从来没有像“checked”=“yes”那样)。我正在考虑更改为 checked="true",因为它看起来更具可读性并且更容易编写 JavaScript。请注意,同样的问题适用于其他属性,例如“禁用”=“禁用”与“禁用”=“真”。只要我保持一致,使用“true”是首选方法吗?谢谢

<input type="checkbox" checked="checked" value="123" name="howdy" />
<input type="checkbox" checked="true"    value="123" name="howdy" />
<input type="checkbox" checked="yes"     value="123" name="howdy" />

回答by Quentin

Only checkedand checked="checked"are valid. Your other options depend on error recovery in browsers.

只有checkedchecked="checked"有效。您的其他选项取决于浏览器中的错误恢复。

checked="yes"and checked="true"are particularly bad as they imply that checked="no"and checked="false"will set the default state to be unchecked… which they will not.

checked="yes"并且checked="true"特别糟糕,因为它们暗示checked="no"并且checked="false"会将默认状态设置为未选中......他们不会。

回答by Flavio Cysne

Accordingly to W3Cchecked input's attribute can be absent/ommited or have "checked" as its value. This does not invalidate other values because there's no restriction to the browser implementation to allow values like "true", "on", "yes" and so on. To guarantee that you'll write a cross-browser checkbox/radio use checked="checked", as recommended by W3C.

因此,W3C检查输入的属性可以不存在/省略或具有“检查”作为其值。这不会使其他值无效,因为对浏览器实现没有限制以允许诸如“true”、“on”、“yes”等值。为保证您将编写跨浏览器的复选框/单选框,请按照W3C 的建议使用 checked="checked" 。

disabled, readonly and ismap input's attributes go on the same way.

disabled、readonly 和 ismap 输入的属性以相同的方式进行。

EDITED

已编辑

empty is not a valid value for checked, disabled, readonly and ismap input's attributes, as warned by @Quentin

正如@Quentin 所警告的,空不是已检查、已禁用、只读和 ismap 输入属性的有效值