Html Safari 不承认“必需”属性。如何解决?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32684657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 18:15:21  来源:igfitidea点击:

Safari is not acknowledging the "required" attribute. How to fix it?

htmlgoogle-chromesafarirequired

提问by Amir Bennasr

<form id="customerForm">
    <label>
        First Name:
        <input id="firstName" required />
    </label>
    <label>
        Social Security Number:
        <input id="ssn" required pattern="^d{3}-d{2}-d{4}$"
            title="Expected pattern is ###-##-####" />
    </label>
    <input type="submit" />
</form>

When I try that document in Chrome, it accepts the conditions and shows the error, as expected.

当我在 Chrome 中尝试该文档时,它接受条件并按预期显示错误。

But when I try that document in Safari, it shows no error.

但是当我在 Safari 中尝试该文档时,它没有显示任何错误。

回答by Jérémie Chazelle

At this time, Safari doesn't support the "required" input attribute. http://caniuse.com/#search=required

目前,Safari 不支持“required”输入属性。 http://caniuse.com/#search=required

To use the 'required' attribute on Safari, You can use 'webshim'

要在 Safari 上使用“required”属性,您可以使用“ webshim

1 - Download webshim

1 - 下载 webshim

2 - Put this code :

2 - 把这个代码:

<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>

回答by sideshowbarker

Currently, Safari doesn't yet emit any error messages for required values in form fields that the user has not provided (nor for invalid values the user has put into form fields). But you can enable it by using hacks or a polyfill. See HTML5 Form Validation Fallback (without a library)for a lightweight hack that enables it, and see h5Validatefor a jQuery-based polyfill plugin.

目前,Safari 尚未针对用户未提供的表单字段中的必需值(也不会为用户输入表单字段中的无效值)发出任何错误消息。但是您可以通过使用 hacks 或 polyfill 来启用它。有关启用它的轻量级 hack,请参阅HTML5 表单验证回退(无库),并参阅h5Validate以了解基于 jQuery 的polyfill插件。

回答by bfulgham

The required attribute should be available in Safari 10.1 and newer.

required 属性应该在Safari 10.1 和更新版本中可用。