Html html5:复选框/单选框中需要命名的属性的意义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4901576/
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
html5: Significance of attribute named required in checkbox/radio
提问by naveen
回答by Emily
Required checkboxes are not unusual. Practically every registration form uses some form of the "I have read and accept the User Agreement" checkbox.
必需的复选框并不少见。实际上,每个注册表单都使用某种形式的“我已阅读并接受用户协议”复选框。
If you have Opera handy try out the code below. The form won't submit unless the checkbox is checked.
如果您手头有 Opera,请尝试下面的代码。除非选中复选框,否则表单不会提交。
<!doctype html>
<html>
<head>
<title>html5</title>
</head>
<body>
<h1>html5 test</h1>
<form action="/">
<input type="checkbox" required="required" id="cb" name="cb">
<label for="cb">required checkbox</label>
<input type="submit">
</form>
</body>
</html>
回答by The Surrican
For checkboxes, the best way is probably to pre-select it and set it to disabled. Just kidding.
对于复选框,最好的方法可能是预先选择它并将其设置为禁用。只是在开玩笑。
To ensure one radio button in a group has been selected, either start with a default choice or validate using javascript. There are no HTML-ways to do that because every possible selection is valid.
要确保选择了一组中的一个单选按钮,请从默认选项开始或使用 javascript 进行验证。没有 HTML 方法可以做到这一点,因为每个可能的选择都是有效的。
In html5 there is a requiredattribute for checkboxes.
在 html5 中,复选框有一个必需的属性。
They are somehow weird, so let me quote something to explain how they work.
它们在某种程度上很奇怪,所以让我引用一些东西来解释它们是如何工作的。
For checkboxes, the required attribute shall only be satisfied when one or more of the checkboxes with that name in that form are checked.
对于复选框,仅选中该表单中该名称的一个或多个复选框时,才应满足所需的属性。
For radio buttons, the required attribute shall only be satisfied when exactly one of the radio buttons in that radio group is checked.
对于单选按钮,只有在检查该无线电组中的单选按钮之一时才应满足所需的属性。
Of course you always have to validate server side because the client can always send you whatever he desires. Just use these methods for better user experience.
当然,您始终必须验证服务器端,因为客户端始终可以向您发送他想要的任何内容。只需使用这些方法即可获得更好的用户体验。
回答by Ronnie Depp
I tested requiredattribute for Radio Buttons today on Firefox 17.0.1 on XP SP2.
我今天在 XP SP2 上的 Firefox 17.0.1 上测试了 Radio Buttons 的必需属性。
It seems to comply with the specification of requiredattribute for radio buttons/groups. As Firefox prompts "Please select one of these options." for both of the code snippets below:
它似乎符合单选按钮/组的必需属性规范。Firefox 提示“请选择这些选项之一”。对于以下两个代码片段:
Either you set requiredattribute for each of the radio buttons
要么为每个单选按钮设置required属性
<input type="radio" name="gender" value="male" required="required" />
<input type="radio" name="gender" value="female" required="required" />
Or Any One of the Radio elements
或任何一种 Radio 元素
<input type="radio" name="color" value="blue" />
<input type="radio" name="color" value="red" required="required" />
<input type="radio" name="color" value="green" />
Any comments and updates are welcome.
欢迎任何评论和更新。
回答by Ben
I just tried it on a radio button in Firefox 4. Adding required
to one radio input, then submitting before selecting one, triggers a "Please select one of these options" tooltip.
我刚刚在 Firefox 4 中的单选按钮上尝试了它。添加required
到一个单选输入,然后在选择一个之前提交,触发“请选择这些选项之一”工具提示。
E.g. this works:
例如这有效:
<input type="radio" name="gender" value="m" required />
<input type="radio" name="gender" value="f" />