简体   繁体   English

不支持的浏览器上的html5必需属性

[英]html5 required attribute on non supported browsers

I have a web application which makes use of the HTML5 required attribute frequently. 我有一个Web应用程序,它经常使用HTML5 required属性。 However Safari and ie 8/9 do not support this attribute. 但是Safari和ie 8/9不支持此属性。 Is there a jQuery plugin that will force the behaviour on non-compatible browsers? 是否有一个jQuery插件会强制不兼容的浏览器上的行为?

This works without any plugin (JQuery only): 这没有任何插件(仅限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>

You could shim it simply... 你可以简单地把它简化一下......

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.
           });
    });
}

You can use h5validate which does exactly what you need. 您可以使用h5validate ,它可以完全满足您的需求。

<script>
$(document).ready(function () {
  $('form').h5Validate();
});
</script>

I wrote a very simple jQuery plugin for this. 我为此编写了一个非常简单的jQuery插件。 It just adds client side validation to a forms using the 'required' attribute. 它只是使用'required'属性向表单添加客户端验证。

https://github.com/garyv/jQuery-Validation-plugin https://github.com/garyv/jQuery-Validation-plugin

After including the plugin, you can call the validate() in jQuery's document ready function or at the end of the html body. 包含插件后,您可以在jQuery的文档就绪函数或html主体的末尾调用validate()。

<script>
  $(document).ready(function(){
    $('form').validate();
  });
</script>

Found a very versatile and lightweight (once minified) engine that works cross-browser including ie8! 发现了一个非常通用和轻量级(一度缩小)的引擎,可以跨浏览器工作,包括ie8!

http://posabsolute.github.io/jQuery-Validation-Engine/ http://posabsolute.github.io/jQuery-Validation-Engine/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM