继手动调用 HTML5 表单验证之后,我现在尝试使用 webshims 在旧版本的 IE 中提供类似 HTML5 的表单验证。
虽然表单验证类型有效(错误显示为需要的字段),但<form onsubmit="return verify(this);" >
不会阻止调用函数 on。我不确定这是否是一个合理的预期。
其次,作为回退,我在 verify() 函数中添加了一个检查,仅当表单有效时才继续。为此,我在 verify() 函数() 中添加了以下代码。
function verify(theForm) {
form = theForm;
if (!form.checkValidity()) {
return false;
}
/* continue with recaptcha processing */
}
我在 IETester 中使用 IE8,因为我没有真正的 IE8 版本可供测试。它的 Javascript 调试似乎表明表单上不存在 checkValidity() 。我认为有一个合理的期望是 checkValidity 应该存在于表单中,因为这是 webshims 的目的。
我在 html 头中的初始化代码是
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="/branding/js-webshim/minified/extras/modernizr-custom.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/modernizr/2.6.2/modernizr.min.js"></script>
<script src="/branding/js-webshim/minified/polyfiller.js"></script>
<script>
$.webshims.polyfill('forms');
</script>
这与记录在案的方法非常接近。并且 webshims 似乎在部分运行,因为缺少必填字段的错误消息正在显示。
我在http://jsfiddle.net/BqW9D/2/添加了一个演示,但我无法在 IE8 或 IE9 的 IETester 中运行 jsfiddle。这只是一团糟。