具有必需和禁用元素的 Html5 表单验证

标签 html forms validation form-submit jquery-form-validator

我意识到你永远不应该禁用一个元素并且还需要它,因为如果它被禁用,用户如何让它有效?

但是,我有一个 <select>我希望我的用户从中选择的产品。当他们选择其中一种产品时,另一种 <select> 只有当产品有一个或多个型号时,才会填充、启用和要求产品型号的。否则,模型选择将被禁用并清空。

虽然我喜欢认为我的代码是完美的并且我可以使模型下拉列表永远不会被禁用并且同时需要,但我并不完美。那么,表单是否能够与 <select> 一起提交?那是禁用的和必需的?

更新

取自 w3.org:

Constraint validation: If the element is required, and its value IDL attribute applies and is in the mode value, and the element is mutable, and the element's value is the empty string, then the element is suffering from being missing.

具体了解“可变”的含义。

A form control can be designated as mutable.

Note: This determines (by means of definitions and requirements in this specification that rely on whether an element is so designated) whether or not the user can modify the value or checkedness of a form control, or whether or not a control can be automatically prefilled.

选择没有值,所以它由它的选项决定。

The select element does not have a value; the selectedness of its option elements is what is used instead.

所以,我认为这意味着如果 <select>元素被禁用并且是必需的,表单可以被认为是有效的?

最佳答案

http://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.


这意味着,当检查表单有效性时,禁用的元素将被“忽略”。

它不会触发任何错误,也不会影响其所属表单的验证状态。

关于具有必需和禁用元素的 Html5 表单验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30532482/

相关文章:

forms - 如何在不改变父级的情况下从子级中删除继承的对象

javascript - 提交时的表单验证 - angularjs 1.5 - 当字段 $pristine 时 $invalid

jquery - 不同操作系统中相同浏览器中的 CSS 渲染

javascript - Google Maps API——添加简单的左转、右转等按钮

jquery - 无法在 jquery UI 对话框中设置输入字段的值

javascript - 如何在禁用 JavaScript 的情况下加载具有特定滚动位置的页面以进行表单提交

asp.net-mvc-3 - 如何在 MVC 中禁用数据注释所需的字段验证器

javascript - 如何在最后以及发布/触发/继续提交之后暂停表单提交,进行最后一次验证?

javascript - html5 视频不支持所有浏览器

html - 如何在 asp.net MVC 中应用完整的背景图像