Anc*_*end 7 javascript ajax jquery
我有一个类似于以下的表格:
<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>
Run Code Online (Sandbox Code Playgroud)
我是AJAX的新手,我想要完成的是当用户点击提交按钮时,我希望mail.php脚本能够在幕后运行而不刷新页面.
我试过类似下面代码的东西,然而,它似乎仍然像以前一样提交表单而不是像我需要它(在幕后):
$.post('mail.php', $('#myForm').serialize());
Run Code Online (Sandbox Code Playgroud)
如果可能的话,我想帮助使用AJAX实现这一点,
提前谢谢了
Kri*_*aes 10
您需要阻止默认操作(实际提交).
$(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();
});
});
Run Code Online (Sandbox Code Playgroud)