简体   繁体   English

HTML5必需的输入属性让我提交一个表单

[英]HTML5 required input attribute lets me submit a form

I have a super simple form: 我有一个超级简单的形式:

<form>
    <input name="name" placeholder="Your name" required>
    <input name="email" type="email" placeholder="Your email" required>
    <input type="submit" value="Submit">
</form>

I'm submitting it using some jQuery (CoffeeScript style): 我正在使用一些jQuery(CoffeeScript样式)提交它:

$('form').submit (event) ->
    do event.preventDefault()

    $.post('../mail.php', $('form').serialize(), (data) ->
        do $('form')[0].reset
    )
    .done -> ...
    .fail -> ...
    .always -> ...

In Chrome (as in Chrome Canary) it works like a charm, and it won't let me submit my form if some of the inputs are empty (it also displays the "Please fill out this field" message, as it should). 在Chrome中(如在Chrome Canary中),它就像一个魅力,如果某些输入为空,它将不允许我提交我的表单(它还会显示“请填写此字段”消息)。 In a lot of other browsers though (Safari, WebKit Safari ie) it lets me submit the form with all the inputs empty. 在很多其他浏览器中(Safari,WebKit Safari ie),它允许我提交表格,所有输入都是空的。

What could the problem be? 问题是什么? I'm a little worried about that event.preventDefault , maybe it stops some browsers from checking the input s? 我有点担心这个事件event.preventDefault ,也许它阻止了一些浏览器检查input

You can't rely on the support of required yet (unfortunately). 您不能依赖required的支持(不幸的是)。 See: http://caniuse.com/#feat=form-validation As you can see Safari is not yet fully supporting this API. 请参阅: http//caniuse.com/#feat=form-validation正如您所见,Safari尚未完全支持此API。

You shouldn't rely on this after all commonly used the browser implemented it either, as a malicous visitor might use a non-default browser and just skip these checks. 在所有常用的浏览器实现之后你不应该依赖于此,因为恶意的访问者可能使用非默认浏览器并且只是跳过这些检查。

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

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