html - 为什么我不能在 radio 输入字段中同时使用 "required"和 "disabled"?

标签 html

我想在单选输入字段中添加 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/

相关文章:

Javascript - 获取没有 ID 或类的元素(按钮)?

html:在粘性导航栏上重叠图片

jquery - 如何创建在您选择基本文本时复制的悬停文本?

html - 如何在两个div之间放置元素(CSS,HTML)

html - CSS:如果我更改缩放比例,背景图像不再对齐

html - 在不同行中显示文本和单选按钮

javascript - 如何让 jQuery 在每次悬停时识别鼠标悬停在图像上一次

html - 如何在 HTML CSS 中打印 60 度旋转水印

ruby-on-rails - 我应该在哪个 HTML5 标签中包装 Rails flash 通知?

javascript - 如果窗口关闭,则打开一个函数