jQuery ajaxStart

时间:2012-04-04 05:52:45

标签: jquery ajax

我正在使用这样的结构:

$('#wait').ajaxStart(function() {
    if ($(this).not(':visible'))
        $(this).showWarning();
}).ajaxComplete(function() {
    if ($(this).is(':visible'))
        $(this).hideWarning();
}).ajaxError(function() {
    alert('Unexpected error...');
});

我希望每次用户点击它时都禁用提交按钮。为了使它具有通用性 - 我想在ajaxStart方法中执行它,那么有没有办法获取按钮的id来初始化请求? 谢谢

2 个答案:

答案 0 :(得分:1)

将所有提交按钮设为一个类,例如:submitBtn,然后

$('.submitBtn').ajaxStart(function() {
  $(this).prop('disabled', true);
}).ajaxComplete(function() {
  $(this).prop('disabled', false);
});

答案 1 :(得分:0)

假设您可以将按钮单击事件传递给此处的函数,您可以这样做:

$('#wait').ajaxSend(function(event) {        //pass event object
  $(event.target).css('disabled','disabled');   //get event target
  if ($(this).not(':visible'))
        $(this).showWarning();
}).ajaxComplete(function() {
    if ($(this).is(':visible'))
        $(this).hideWarning();
}).ajaxError(function() {
    alert('Unexpected error...');
});
相关问题