Html5表单验证,包含必需和禁用的元素 [英] Html5 form validation with required and disabled element

查看:159
本文介绍了Html5表单验证,包含必需和禁用的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



然而,我有一个< select> 我希望我的用户可以选择的产品。当他们选择其中一个产品时,仅当产品具有一个或多个型号时,产品型号的另一个< select> 才会被填充,启用和必需 STRONG>。



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

更新

取自w3.org:
$ b blockquote>

约束验证: 如果元素是必需的,并且其值IDL属性适用且处于mode值,且元素是可变的,并且元素的值是空字符串,则该元素正在遭受。

查看具体是什么意思是可变的。


表单控件可以被指定为mutable。

注意:这决定了(通过本规范中的定义和要求它依赖于是否指定了一个元素),用户是否可以修改表单控件的值或检查,或者是否可以自动预先填充控件。

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


select元素没有值;选择元素的选择是用来代替的。


所以,我认为这意味着如果一个 < select> 元素被禁用并且被要求,表单可以被认为是有效的? href =http://www.w3.org/TR/html5/forms.html#enabling-and-disabling-form-controls:-the-disabled-attribute =noreferrer> http:// www .w3.org / TR / html5 / forms.html#enabled-and-disableabling-form-controls:-the-disabled-attribute :


约束验证:如果某个元素处于禁用状态,则会禁止进行约束验证。

http://www.w3.org/TR /html5/forms.html#barred-from-constraint-validation


可提交元素是候选人用于约束验证除外当一个条件有禁止约束验证的元素

最后,从约束验证列表步骤, http://www.w3.org/TR/html5/forms .html#constraint-validation


3.1:如果字段不是约束验证的候选字段,则转到下一个元素。







这意味着,一个禁用的元素将被传递当检查表单有效性。



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


I realize that you should never disable an element and also require it, because how could the user make it valid if it's disabled?

However, I have a <select> of products that I want my users to choose from. When they select one of the products another <select> for the product's models is populated, enabled, and required only if the product has one or more models. Otherwise, the model select is disabled and emptied.

While I like to think that my code is perfect and I can make it so that the model dropdown is never disabled and required at the same time, I'm not perfect. So, would the form be able to be submitted with a <select> that is disabled and required?

Update

Taken from 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.

Looked up specific of what it means to be "mutable".

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.

Selects don't have a value, so it is determined by it's options.

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

So, I think this means that if a <select> element was disabled and required, the form could be considered valid?

解决方案

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.

And finally, from the list of Constraint validation steps, 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.


That means, a disabled element will just be "passed over" when form validity is checked.

It doesn’t trigger any errors, and has no influence whatsoever on the validation state of the form it belongs to.

这篇关于Html5表单验证,包含必需和禁用的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆