javascript – AJAX:提交表单而不刷新页面

栏目: jQuery · 发布时间: 7年前

内容简介:翻译自:https://stackoverflow.com/questions/14235596/ajax-submitting-a-form-without-refreshing-the-page

我有一个类似于以下的表格:

<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();
    });
});

翻译自:https://stackoverflow.com/questions/14235596/ajax-submitting-a-form-without-refreshing-the-page


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

计算机程序设计艺术 第2卷 半数值算法(第3版)(英文影印版)

计算机程序设计艺术 第2卷 半数值算法(第3版)(英文影印版)

(美)Donald E.Knuth / 清华大学出版社 / 2002-09-01 / 83.0

计算机程序设计艺术:英文版(第2卷 半数值算法),ISBN:9787302058151,作者:(美)Donald E. Knuth著一起来看看 《计算机程序设计艺术 第2卷 半数值算法(第3版)(英文影印版)》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具