Html 正确的方法来做 HTML5 复选框

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

Correct way to do HTML5 checkbox

html

提问by Skizit

I can't seem to find an example anywhere... what's the correct way of doing a HTML5 checkbox?

我似乎在任何地方都找不到示例......做 HTML5 复选框的正确方法是什么?

回答by Pekka

As far as I know and the docs state, nothing fundamental has changed. The basic markup is

据我所知和文档状态,没有什么基本的改变。基本标记是

<input name="your_name" value="your_value" type="checkbox">

What is new is some interesting properties.

新的是一些有趣的属性。

  • form- a reference to the form the control is associated with (nice!)
  • autofocus- to be focused as soon as the document is loaded (nice!)
  • required- require that it be checked (super nice! Although it isn't supported by Internet Explorer or Safari (yet).)
  • form- 对与控件关联的表单的引用(很好!)
  • autofocus- 加载文档后立即聚焦(很好!)
  • required- 要求对其进行检查(非常好!虽然 Internet Explorer 或 Safari(尚)不支持它)。

回答by wolfstevent

A more complete example - and avoiding the long stream of posts to How to check whether a checkbox is checked in jQuery?.

一个更完整的示例 - 并避免在 jQuery 中如何检查复选框是否被选中的冗长的帖子流.

HTML

HTML

<input id="your_id" name="your_name" value="your_value" type="checkbox">

Optionally add the 'checked' attribute to default to checked on load.

(可选)将“已检查”属性添加到默认为在加载时已检查。

<input id="your_id" name="your_name" value="your_value" type="checkbox" checked>

JavaScript

JavaScript

$('#your_id').is(':checked')        // Returns a Boolean TRUE if checked

e.g.

例如

if ($('#your_id').is(':checked')) {
    // Checkbox was checked
}