[英]Reset individual form field's custom validity state
I have a basic form field that is valid in all conditions including when empty:我有一个在所有条件下都有效的基本表单字段,包括为空时:
<input id="example1" name="example" type="text" />
In some unrelated (to this question) conditions I use JavaScript to mark this element as invalid:在一些不相关(与这个问题)的情况下,我使用 JavaScript 将此元素标记为无效:
id_('example1').setCustomValidity('All aboard the nope train!');
id_('example1').reportValidity();
I won't post the other code because it is perfectly valid if this form field is empty when the full form is submitted.我不会发布其他代码,因为如果在提交完整表单时此表单字段为空,则它是完全有效的。
How do I reset the validity state of this specific form field without attempting to submit the entire form?如何在不尝试提交整个表单的情况下重置此特定表单字段的有效性 state?
I have attempted id_('post_custom_selector').validity.valid = true;
我尝试过
id_('post_custom_selector').validity.valid = true;
though it did not work and with all the methods for ValidityState being read-only I suspect that if I'm not missing anything else looking at the methods for the HTMLInputElement then I may be stuck having to duplicate the node and replace it.尽管它不起作用并且ValidityState的所有方法都是只读的,但我怀疑如果我没有遗漏任何其他东西来查看HTMLInputElement的方法,那么我可能不得不复制节点并替换它。
Absolutely no frameworks or libraries.绝对没有框架或库。
If the first parameter for setCustomValidity
is empty (it still must be set) then it will reset the validity of the element :如果
setCustomValidity
的第一个参数为空(仍然必须设置),那么它将重置元素的有效性:
id_('post_custom_selector').setCustomValidity('');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.