必需 属性在 Safari 中不起作用。
为了解决这个问题,我使用了 Webshim。一切正常,直到我注意到当我在表单标签上使用 onsubmit 时,它会在您提交时触发,无论是否有一些必需的输入为空。 您在输入中看到“请填写此字段”,但无论如何都会触发 onsubmit 函数。
<form onsubmit="alert('hi')" action="#">
<input placeholder="native input works" required>
<input type="submit">
</form>
这是我初始化 Webshim 的方式:
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/webshim/1.16.0/dev/polyfiller.js"></script>
<script>
webshim.activeLang('en');
webshims.polyfill('forms');
webshims.cfg.no$Switch = true;
</script>
这里有一个工作示例。您可以在 Safari 和 Chrome 中尝试,您会看到警报在 safari 上触发,而不是在 chrome 中。
https://jsfiddle.net/3pxsvxxj/
拜托,我需要解决这个问题,但我找不到办法。
干杯。
最佳答案
如您所说,Safari 浏览器不支持 required
属性,如所列 here .
如果你引用this问题,它有提供该功能的替代方案。
既然你说你正在使用 webshim,请参阅 this我链接的问题的答案提供了使用 webshim 的示例:
At this time, Safari doesn't support the "required" input attribute. http://caniuse.com/#search=required
To use the 'required' attribute on Safari, You can use 'webshim'
1 - Download webshim
2 - Put this code :
<head>
<script src="js/jquery.js"></script>
<script src="js-webshim/minified/polyfiller.js"></script>
<script>
webshim.activeLang('en');
webshims.polyfill('forms');
webshims.cfg.no$Switch = true;
</script>
</head>
关于javascript - Onsubmit 函数在 Safari 上触发,即使所需的输入为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42286176/