我有一个类似于以下的表格:
<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
脚本在后台运行,无需刷新页面。
我尝试了类似下面的代码,但是,它似乎仍然像以前一样提交表单,而不是像我需要的那样(在幕后):
$.post('mail.php', $('#myForm').serialize());
如果可能的话,我想获得使用 AJAX 实现此功能的帮助,
提前谢谢了
您需要防止默认操作(实际提交)。
$(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();
});
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)