1

我在进行 Ajax 调用的复选框上有一个 jQuery 更改事件,然后突出显示该复选框所属的表行。当 Ajax 调用报告错误时,我遇到了问题 - 在这种情况下,我想反转复选框单击(我正在that.prop("checked", !that.prop("checked"))这样做)。但是,发生的事情是它进入了调用更改事件的无限循环。如您所见,为了阻止这种行为,我尝试设置一个名为 ajaxInProgress 的变量,但它似乎并没有像我希望的那样工作。任何人都可以提供替代方法或发现错误吗?我不确定我重置复选框的方式是否必要 - 是否可以重置复选框并仍然阻止更改事件启动?

var ajaxInProgress = false; //add this to stop recursive calls when errors occur 
jQuery(document).ready(function() {
  jQuery(".cc").change(function (e) {

    if (ajaxInProgress) return;

    var mode = "remove"
    if (jQuery(this).attr("checked")) {
       mode = "add"
    } 

    //grab the application ID from the checkbox ID
    var thisApp = this.id.substr(3);
    var that = jQuery(this);
    ajaxInProgress = true;
    jQuery.get("/particular/edit-ajax", { application_id: thisApp, 
                                          party_id: 1920, 
                                          party_rel_id: 21,
                                          mode: mode} , function(response) {
            if (response == 0) {
              that.closest('tr').removeClass('even').removeClass('odd').addClass('highlight');//highlight the tr
            } else {
              e.preventDefault();
              that.prop("checked", !that.prop("checked")); //reset the checkbox if there was an error
              alert("Please contact support - couldn't update database for application  "+thisApp+". Response was: "+response);
            }
    });       
    ajaxInProgress = false;
  });
});
4

3 回答 3

2

这一切似乎都非常复杂,而且我会这样做:

(function($) {
    $(function() {
        function sendAjax(e) {
            var data = {
                application_id: this.id.substr(3), 
                party_id: 1920, 
                party_rel_id: 21,
                mode: this.checked?'add':'remove',
            },
                that=this;

            $.ajax({
                type: 'GET',
                data: data
            }).done(function() {
                $(that).closest('tr').removeClass('even odd').addClass('highlight');
            }).fail(function(response) {
                e.preventDefault();
                that.checked = !that.checked;
                alert("Please contact support - couldn't update database for application  "+that.id.substr(3)+". Response was: "+response);
            }).always(function() {
                $(".cc").one('change', sendAjax);
            });
       }
       $(".cc").one('change', sendAjax);
    });
})(jQuery);
​
于 2012-10-15T16:51:39.487 回答
1

可能是因为你有ajaxInProgress = false;jQuery.get。这会将它设置为false在您从 ajax 请求中获取任何内容之前。将该行移到回调内部:

jQuery.get("/particular/edit-ajax",
    {
        application_id: thisApp,
        party_id: 1920,
        party_rel_id: 21,
        mode: mode
    },
    function(response) {
        ajaxInProgress = false;

        if (response == 0) {
            that.closest('tr').removeClass('even').removeClass('odd').addClass('highlight');//highlight the tr
        } else {
          e.preventDefault();
          that.prop("checked", !that.prop("checked")); //reset the checkbox if there was an error
          alert("Please contact support - couldn't update database for application  "+thisApp+". Response was: "+response);
        }
    }
);

小提琴:http: //jsfiddle.net/gromer/BgNfR/

我的 Fiddlewindow.setTimeout用于模拟 ajax 调用。我认为我的解决方案不起作用,但这是因为我最初使用window.setInterval它,所以看起来它被一遍又一遍地调用,它只是没有像我应该那样模拟 ajax 请求。

于 2012-10-15T16:39:09.943 回答
0

使用“处理中”变量为真:

var ajaxInProgress = false, handlingInProgress = false; //add this to stop recursive calls when errors occur
jQuery(document).ready(function() {
  jQuery(".cc").change(function (e) {
    if (handlingInProgress) return;
    if (ajaxInProgress) return;

    var mode = "remove"
    if (jQuery(this).attr("checked")) {
       mode = "add"
    } 

    //grab the application ID from the checkbox ID
    var thisApp = this.id.substr(3);
    var that = jQuery(this);
    ajaxInProgress = true;
    jQuery.get("/particular/edit-ajax", { application_id: thisApp, 
                                          party_id: 1920, 
                                          party_rel_id: 21,
                                          mode: mode} , function(response) {
            if (response == 0) {
              that.closest('tr').removeClass('even').removeClass('odd').addClass('highlight');//highlight the tr
            } else {
              e.preventDefault();
              that.prop("checked", !that.prop("checked")); //reset the checkbox if there was an error
              handlingInProgress = true; // Please see not below
              alert("Please contact support - couldn't update database for application  "+thisApp+". Response was: "+response);
            }
    });       
    ajaxInProgress = false;
    handlingInProgress = false; // 
  });
});

当您更改复选框时,事件会再次触发,但由于handlingInProgress设置为 true,它会立即脱离处理程序。

于 2012-10-15T16:36:03.123 回答