问题描述
输入字段必填有一个新的HTML5属性,当提交表单并且该字段为空时会弹出一个气泡消息.有没有办法用不同的文字弹出同一个气泡?我想将它用于不同的验证(不仅仅是它是否为空).
There is a new HTML5 attribute for input field required which pops up a bubble message when submitting form and the field is empty. Is there any way of popping up the same bubble with different text? I want to use it for different validations (not just if it's empty or not).
这是 Chrome 中此气泡(弹出窗口)的屏幕截图:
Here is a screenshot on this bubble (pop-up) in Chrome:
推荐答案
HTML5 提供了一个名为 setCustomValidity 来实现这一点.此方法是一个低级 API,用于添加新的验证约束和添加新的/更改错误消息.您必须调用您的脚本,该脚本会删除/添加验证消息 - 使用 setCustomValidity -(在DOMready"上和)输入/更改.
HTML5 provides a method called setCustomValidity to achieve exactly this. This method is a low level API to add new validation constraints and to add new/change errormessages. You have to call your script, which removes / adds the validation message - using setCustomValidity - (on "DOMready" and) on input/change.
我写了一个polyfill脚本,它不仅在所有浏览器中实现了那些HTML5表单方法,还添加了一个"customValidity"-helper,这使得使用自定义消息添加自定义验证约束变得更加容易.
I have written a polyfill script, which not only implements those HTML5 form methods in all browsers, but also adds a "customValidity"-helper, which makes it more easy to add custom validation constraints with custom messages.
注意:您无需实现 webshims 库即可使用此脚本.它也可以在没有 webshims 库的情况下工作.(但如果您使用 webshims lib,您将在包括 IE6 在内的所有浏览器中获得约束验证).如果您不想拥有这个跨浏览器,只需使用 有效性帮助脚本.文档在这里.
Note: You don't need to implement webshims lib to make use of this script. It also works without webshims lib. (But if you use webshims lib, you will get constraint validation in all browsers including IE6). If you don't want to have this cross-browser, simply take only the validity-helper script. Documentation is here.
请注意,如果您确实通过设置自定义消息对消息进行了更改,则必须包含一种清除消息的方法,例如
Note that if you do make a change to the message by setting a custom message, you have to include a way to clear the message such as
myFormElement.setCustomValidity('');
myFormElement.setCustomValidity('');
如果您不清除消息,表单元素将被视为无效,表单将不会提交.
if you don't clear the message, the form element will be seen as invalid and the form will not submit.
这篇关于HTML5 气泡消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!





大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)