zoukankan      html  css  js  c++  java
  • Ajax beforeSend和complete 方法与防止重复提交

    $.ajax({
        beforeSend: function(){
         // Handle the beforeSend event
        },
        complete: function(){
         // Handle the complete event
        }
        // ......
    });

    其中的$.ajax请求中有一个beforeSend方法,用于在向服务器发送请求前执行一些动作。

    complete 方法执行完后调用

    用途

    防止重复数据

    // 提交表单数据到后台处理
    $.ajax({
        type: "post",
        data: studentInfo,
        contentType: "application/json",
        url: "/Home/Submit",
        beforeSend: function () {
            // 禁用按钮防止重复提交
            $("#submit").attr({ disabled: "disabled" });
        },
        success: function (data) {
            if (data == "Success") {
                //清空输入框
                clearBox();
            }
        },
        complete: function () {
            $("#submit").removeAttr("disabled");
        },
        error: function (data) {
            console.info("error: " + data.responseText);
        }
    });

    模拟Toast效果

    $.ajax({
        type: "post",
        contentType: "application/json",
        url: "/Home/GetList",
        beforeSend: function () {
            $("loading").show();
        },
        success: function (data) {
            if (data == "Success") {
                // ...
            }
        },
        complete: function () {
            $("loading").hide();
        },
        error: function (data) {
            console.info("error: " + data.responseText);
        }
    });
  • 相关阅读:
    bzoj3339 rmq problem (range mex query)
    sg函数与博弈论
    JSOI Round 2题解
    bzoj2286 消耗战
    输入输出优化(黑科技)
    虚树
    迷之数学题*2
    k-d tree 学习笔记
    一类有依赖的树形背包dp方法
    BZOJ4283: 魔法少女伊莉雅(最短路径图+最短路径树)
  • 原文地址:https://www.cnblogs.com/axu92312/p/6101567.html
Copyright © 2011-2022 走看看