jquery.one()触发了一次查询,但是如果.one(()内部出现问题,我怎么能触发一次查询呢

jquery .one() fire query for one time but how can i able to fire query once if something is going wrong inside .one()?

本文关键字:一次 查询 one 问题 怎么能 如果 jquery 内部      更新时间:2023-09-26

我有一个登录页面,即使用户多次点击,我也想提交一次凭据。这就是为什么我想使用.one(),但是.one()有一个问题,假设用户有一些凭据问题,那么下次他修复它并尝试再次发送时。one()将不起作用。然后你必须做一些步骤。如何解决这个问题?

$(document).ready(function(){
$("#submit").one("click", function() {
    data = some data
    if data !==""{
        $.ajax({
            url: '/my url/',
            type: 'POST',                   
            contentType: 'application/json',
            dataType: 'json',
            data :data,
            processData: false,
            success: function(data){
                alert("done")   
            },
            error: function(jqXHR, textStatus, errorThrown){
                alert("Some Error")      
                //here i want to do something so that if user click the submit button it will fire once                       
            }
        })
    }
    else
        //data is not there
        //here i want to do something so that if user click the submit button it will fire once 
    });

您可以再次绑定处理程序:

$("#submit").one("click", function handler() { // give the function a name
    // keep a reference to the clicked element so that you can access it
    // inside callbacks
    var self = this; 

    // ...
    // when something went wrong, bind the handler again
    $(self).one('click', handler);
});

您希望使用one()来实现这一点,但使用简单的ajax。与现在的方法不同,创建一个名为"allowAjaxCheckLogin"的变量,并将其设置为true。在您的常规点击处理程序中,您可以检查它是否为true,如果是,请将其设置为false,然后执行ajax部分。当ajax完成并且用户需要能够再次执行时,将其设置回真正的


var allowAjaxCheckLogin = true; // set to true, normal/startsituation allows a click
$('#submit').on('click', function(){
    // User may trigger actions:
    if( allowAjaxCheckLogin === true){
        allowAjaxCheckLogin = false; // Instantly set to false, if the user clicks again, nothing will happen
        $.ajax({ /* ajax settings here */ })
        .complete(function(){
            // this always fires, here you can check if you need to refresh the page to anochter location
           allowAjaxCheckLogin = true; // set back so the user may trigger again
        });
    }
});

您可以使用on()off()进行类似的

var inProgress = false;
$("#submit").on("click", function() {
    data = some data
    if(data !== "" && !inProgress ){
        inProgress = true;
        $.ajax({
            url: '/my url/',
            type: 'POST',                   
            contentType: 'application/json',
            dataType: 'json',
            data :data,
            processData: false,
            success: function(data){
                $('#submit').off();  // If is correct remove all event listeners 
            },
            error: function(jqXHR, textStatus, errorThrown){
                loginFailed();                  
            }
        })
    }
    });
var loginFailed = function(){
   inProgress = false;
   alert("Some Error");
   // Display any Errors 
};

关闭:http://api.jquery.com/off

在:http://api.jquery.com/on

我们创建一个变量来检查是否有登录正在进行中,如果没有,我们可以尝试再次登录,如果有,则单击时不会发生任何事情,如果登录成功,则事件侦听器将被删除。