我想在单选输入字段中添加 required
属性,这是我的代码
<!doctype html>
<html>
<head>
<meta charset='utf-8'>
<title>test radio</title>
<style>
div.radio{
background-color:silver;
}
</style>
</head>
<body>
<form action='procesPost.html' method='POST'>
<div class='radio'>
<input type='radio' required name='test' value=1>
<input type='radio' required name='test' value=2>
</div>
<input type='SUBMIT' value='submit'>
</form>
</body>
</html>
效果很好,我必须选择一个 radio 来提交,但是,如果我想禁用一个 radio ,required
约束将不起作用
<!doctype html>
<html>
<head>
<meta charset='utf-8'>
<title>test radio</title>
<style>
div.radio{
background-color:silver;
}
</style>
</head>
<body>
<form action='procesPost.html' method='POST'>
<div class='radio'>
<input type='radio' required name='test' value=1>
<input type='radio' required disabled name='test' value=2>
</div>
<input type='SUBMIT' value='submit'>
</form>
</body>
</html>
现在,即使我没有选择任何电台,我也可以提交表单。 我想知道为什么会发生这种情况,我该如何解决这个问题?
我在 FierFox 32.0.3、RHEL6.2 中测试了我的代码
最佳答案
根据 CBroe 的说法:
www.w3.org/TR/html5/forms.html#enabling-and-disabling-form-controls:-the-disabled-attribute:
Constraint validation: If an element is disabled, it is barred from constraint validation.
http://www.w3.org/TR/html5/forms.html#barred-from-constraint-validation :
A submittable element is a candidate for constraint validation except when a condition has barred the element from constraint validation.
最后,从约束验证步骤列表中,http://www.w3.org/TR/html5/forms.html#constraint-validation :
3.1: If field is not a candidate for constraint validation, then move on to the next element.
这意味着,当检查表单有效性时,禁用的元素将被“忽略”。
它不会触发任何错误,也不会影响其所属表单的验证状态。
评论在此链接中。 https://stackoverflow.com/a/30533215
关于html - 为什么我不能在 radio 输入字段中同时使用 "required"和 "disabled"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28145654/