我有一个网络应用程序,经常使用HTML5的required
属性。然而,Safari和IE 8/9不支持此属性。是否有一个jQuery插件可以强制非兼容浏览器的行为?
这可以不使用任何插件(仅使用JQuery)实现:
<script>
// fix for IE < 11
if ($("<input />").prop("required") === undefined) {
$(document).on("submit", function(e) {
$(this)
.find("input, select, textarea")
.filter("[required]")
.filter(function() { return this.value == ''; })
.each(function() {
e.preventDefault();
$(this).css({ "border-color":"red" });
alert( $(this).prev('label').html() + " is required!");
});
});
}
</script>
if ($("<input />").prop("required") === undefined) {
$(document).on("submit", function(event) {
$(this)
.find("input, select, textarea")
.filter("[required]")
.filter(function() { return this.value; })
.each(function() {
// Handle them.
});
});
}
.filter(function() { return this.value; })
不仅会排除所有空表单字段,而是排除所有空表单字段,这样你最终得到的是所有必填字段且有内容的字段,而不是所有必填字段缺少内容的字段。 - S.B.<script>
$(document).ready(function () {
$('form').h5Validate();
});
</script>
<script>
$(document).ready(function(){
$('form').validate();
});
</script>