简体   繁体   English

用AJAX提交HTML 5表单?

[英]Submit a HTML 5 Form with AJAX?

I got some trouble submitting a form with ajax. 用ajax提交表单时遇到了麻烦。

Since HTML5 Form already have "required" validation within the input tag, there is no need for javascript validation check. 由于HTML5表单已经在输入标记中进行了“必需”验证,因此无需进行JavaScript验证检查。 However, because of that, I don't know how to use javascript(jQUERY) to submit a form AFTER those validation checks have been passed. 但是,因此,在通过这些验证检查之后,我不知道如何使用javascript(jQUERY)提交表单。 In other words, how do I know whether the validations checks has passed or not? 换句话说,我怎么知道验证检查是否已经通过?

eg : ??condition?? 例如:条件? (What condition should I put before loading the submitted page)? (在加载提交的页面之前我应该​​处于什么条件)? ("div").load(finishSubmittingForm.html) )}; (“ div”)。load(finishSubmittingForm.html))};

Any idea? 任何的想法?

Thanks 谢谢

There are some API methods available. 有一些可用的API方法。 See developer.mozilla.org . 请参阅developer.mozilla.org

However, I would strongly advise you think carefully before relying on HTML5 validation. 但是,我强烈建议您在依赖HTML5验证之前请仔细考虑。 It definitely has some major drawbacks at this point in time, as it is still "in its infancy". 由于它仍处于“婴儿期”,因此它目前肯定具有一些主要缺点。 I would say that for now, you would be better off using a JS validation library; 我要说的是,现在,使用JS验证库会更好。 jQuery Validation is excellent. jQuery验证非常出色。

from specification: 从规格:

If the user presses the submit button. 如果用户按下提交按钮。 The browser has to first validate the form and if the form is valid, the submit event is fired. 浏览器必须首先验证表单,如果表单有效,则将引发Submit事件。

This means, that you can simply hook into the form's submit event and start your ajax. 这意味着,您可以简单地挂接到表单的Submit事件并启动ajax。

$('form').bind('submit', function(){
    //Ajax implementation here
});

But, Opera has a really nasty bug here. 但是,Opera在这里有一个非常讨厌的错误。 Opera first fires the submit event and then validates the form. Opera首先触发Submit事件,然后验证表单。

You can workaround this, by using the validation methods of HTML5. 您可以通过使用HTML5的验证方法来解决此问题。 This would look like this: 看起来像这样:

$('form').bind('submit', function(){
    if(!this.checkValidity || this.checkValidity()){
        //Ajax implementation here
    }
});

About the current state of the HTML5 form spec. 关于HTML5表单规范的当前状态。 There are some parts, that should extend the form spec. 有一些部分应该扩展形式规格。 The features, which are currently implemented in FF4 and others (FF4 has less form features than Opera and Chrome, but very good implementation) are stable enough for production. 这些功能目前已在FF4和其他功能中实现(FF4具有比Opera和Chrome少的形式功能,但非常好的实现)对于生产来说足够稳定。

If you want to use HTML5 forms in all browsers, I would suggest the webshims HTML5 forms polyfill . 如果要在所有浏览器中使用HTML5表单,建议使用webshims HTML5表单polyfill It not only polyfills the incapable browsers, but also fixes some bugs in browsers, which haven't implemented the form feature according to the spec. 它不仅可以填充无法使用的浏览器,还可以修复浏览器中的一些错误,这些错误尚未根据规范实现表单功能。 For example, the Opera bug mentioned above will be automatically fixed. 例如,上面提到的Opera错误会自动修复。

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

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