如何在ajax调用之前禁用一个函数,并在ajax成功时启用它?

时间:2014-10-01 20:25:37

标签: javascript jquery ajax click

我有一个函数是在ajax函数之外,我想禁用它"启用"在ajax运行时单击函数,并在ajax成功加载后启用它,我该如何准确处理?任何帮助,将不胜感激。 这是我的代码:

 $('enable').click(function () {
        console.log(123);
        $(this).css('display', 'none');

 var xhr =  $.ajax({
        type: "POST",
        url: $.vp + "signup/distLookup.ashx" + "?first=" + val_f_name + "&last=" + val_l_name + "&country=" + val_country + "&language=" + $.langID,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) { 
            console.log(data);

    ; 

4 个答案:

答案 0 :(得分:1)

var enable = true;
$('enable').click(function () {
     if(enable) {
        enable = false;
        console.log(123);
        $(this).css('display', 'none');

         var xhr =  $.ajax({
            type: "POST",
            url: $.vp + "signup/distLookup.ashx" + "?first=" + val_f_name + "&last=" +     val_l_name + "&country=" + val_country + "&language=" + $.langID,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                console.log(data);
                enable = true;
            }
        });
    }
});

当ajax触发时,您可以将enable设置为false。

编辑:

这样的事情?

var enable = true;
$('enable').click(function () {
     if(enable) {
        console.log(123);
        $(this).css('display', 'none');
     }
});

function callAjax() {
    enable = false;
    var xhr =  $.ajax({
        type: "POST",
        url: $.vp + "signup/distLookup.ashx" + "?first=" + val_f_name + "&last=" +     val_l_name + "&country=" + val_country + "&language=" + $.langID,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
            console.log(data);
            enable = true;
        }
    });
}

答案 1 :(得分:0)

如果ajax在click函数内,则:

$('enable').click(function () {
     console.log(123);
     $(this).hide();
     var that=this;         

     var xhr =  $.ajax({
            type: "POST",
            url: $.vp + "signup/distLookup.ashx" + "?first=" + val_f_name + "&last=" + val_l_name + "&country=" + val_country + "&language=" + $.langID,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) { 
                console.log(data);
                $(that).show();
            }
     });
});

否则:

$('enable').bind('click',function () {
     console.log(123);
     $(this).unbind('click');
});

     var xhr =  $.ajax({
            type: "POST",
            url: $.vp + "signup/distLookup.ashx" + "?first=" + val_f_name + "&last=" + val_l_name + "&country=" + val_country + "&language=" + $.langID,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) { 
                console.log(data);
                $('enable').bind('click',function () {
                    console.log(123);
                    $(this).unbind('click');
                });
            }
     });

答案 2 :(得分:0)

尝试使用.one代替.on,然后在success附加处理程序。

function sendRequest() {
  var xhr = $.ajax({
    type: "POST",
    url: $.vp + "signup/distLookup.ashx" + "?first=" + val_f_name + "&last=" + val_l_name + "&country=" + val_country + "&language=" + $.langID,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (data) { 
      console.log(data);
      $('enable').one('click', sendRequest); // We restore it when get success
    }
}

$('enable').one('click', sendRequest); // once you clicked your handler would be removed

答案 3 :(得分:0)

当你想要禁用点击事件时,你可以拥有一个标志:

$('enable').click(function () {
    if ($(this).data('disabled')) return;

    $(this).data('disabled', true);

    $.ajax({ ... }).always(function() { $(this).data('disabled', false); }.bind(this));
});

使用.always()确保在ajax请求失败时启用它。