Html iPad/iPhone 上“必需”的 Html5 表单元素不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10664356/
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 00:31:33  来源:igfitidea点击:

Html5 form element "required" on iPad/iPhone doesn't work

htmlformsmobile-safarirequiredhtml-validation

提问by SongBox

iPad safari is supposed to be html5 compliant, but it seems that the required element doesn't work. Anyone know why, or have a decent workaround that doesn't require a ton of JavaScript?

iPad safari 应该是 html5 兼容的,但似乎所需的元素不起作用。任何人都知道为什么,或者有一个不需要大量 JavaScript 的体面解决方法?

My code

我的代码

<input type=email class=input placeholder="Email" name="email" required>

回答by Ian Devlin

It's not supported in iOS yet: when can I use: required.

iOS 尚不支持它:我什么时候可以使用: required

回答by Eliot

This is a jQuery solution to the issue, it highlights the input fields that have failed in a pinky colour too.

这是该问题的 jQuery 解决方案,它也以粉红色突出显示失败的输入字段。

$('form').submit(function(){
    var required = $('[required="true"]'); // change to [required] if not using true option as part of the attribute as it is not really needed.
    var error = false;

    for(var i = 0; i <= (required.length - 1);i++)
    {
        if(required[i].value == '') // tests that each required value does not equal blank, you could put in more stringent checks here if you wish.
        {
            required[i].style.backgroundColor = 'rgb(255,155,155)';
            error = true; // if any inputs fail validation then the error variable will be set to true;     
        }
    }

    if(error) // if error is true;
    {
        return false; // stop the form from being submitted.
    }
});

回答by Matou? Be?vá?

Since iOS 10.3 this atrributes are supported. Also e-mail type require writing the @ symbol and so on...

从 iOS 10.3 开始支持此属性。电子邮件类型也需要写@符号等等......

回答by AgnesCat

I guess you can do something before the submit action like this

我想你可以在像这样提交操作之前做一些事情

<form name="myForm" action="valid.html" onsubmit="checkValid()" method="post">
  ... ...
</form>

after pressing submitbutton, checkValid()is evoked before it actually submits. a return value of truewill continue the submit action.

按下submit按钮后,checkValid()在实际提交之前被唤起。的返回值true将继续提交操作。

refer to this postfor further explanation.:)

请参阅这篇文章以获得进一步的解释。:)

回答by Jbrown

If you are already using jQuery, Modernizr, and yepnope, this is one way to deal with it. If you aren't then this will add a lot of extra javascript.

如果您已经在使用 jQuery、Modernizr 和 yepnope,这是处理它的一种方法。如果你不是那么这将添加很多额外的 javascript。

My solution

我的解决方案

回答by C.Tom

If you use PHP, you can add a validation like this

如果你使用 PHP,你可以添加这样的验证

function validation(){

  if(isset($_POST['submit'])){
  $email = $_POST['email'];

     if(empty($email)){
        echo $error = "Your email cannot be empty";

      } else {
        return true; //or do something next here
     }
   }

You then add this function in php before your form.

然后在表单之前在 php 中添加此函数。