AJAX:无需刷新页面提交表单 [英] AJAX: Submitting a form without refreshing the page
本文介绍了AJAX:无需刷新页面提交表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有类似下面这样的形式:
I have a form similar to the following:
<form method="post" action="mail.php" id="myForm">
<input type="text" name="fname">
<input type="text" name="lname">
<input type="text" name="email">
<input type="submit">
</form>
我是新来的AJAX和我试图做到的是,当用户点击提交按钮,我想为 mail.php
脚本背后的运行场景而无需刷新页面。
I am new to AJAX and what I am trying to accomplish is when the user clicks the submit button, I would like for the mail.php
script to run behind the scenes without refreshing the page.
我想是这样的code以下,但它似乎仍然像以前一样提交表单,而不是像我需要它(幕后):
I tried something like the code below, however, it still seems to submit the form as it did before and not like I need it to (behind the scenes):
$.post('mail.php', $('#myForm').serialize());
如果有可能,我希望得到帮助实施这一使用AJAX,
If possible, I would like to get help implementing this using AJAX,
在预先感谢
推荐答案
您需要prevent默认操作(实际提交)。
You need to prevent the default action (the actual submit).
$(function() {
$('form#myForm').on('submit', function(e) {
$.post('mail.php', $(this).serialize(), function (data) {
// This is executed when the call to mail.php was succesful.
// 'data' contains the response from the request
}).error(function() {
// This is executed when the call to mail.php failed.
});
e.preventDefault();
});
});
这篇关于AJAX:无需刷新页面提交表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文