Ajax, prevent multiple request on click

前端 未结 10 2235
忘掉有多难
忘掉有多难 2020-11-28 04:10

I\'m trying to prevent multiple requests when user click on login or register button. This is my code, but it doesn\'t work. Just the first time works fine, then return fals

10条回答
  •  渐次进展
    2020-11-28 05:05

    Or you can do it by $(this).addClass("disabled"); to you button or link and after click is performed, you can $(this).removeClass("disabled");.

    // CSS

    .disabled{
    cursor: not-allowed;
    
    }
    

    // JQUERY

    $('#do-login').click(function(e) {
       e.preventDefault();
    
        $(this).addClass("disabled");
        $.ajax({
            type: "POST",
            url: "/php/auth/login.php",
            data: $("#login-form").serialize(),
            context: this,
            success: function(msg) {
        //do more here
    
               $(this).removeClass("disabled");
            },
        });
    });
    

    P.S. If you use bootstrap css, you do not need the css part.

提交回复
热议问题