2012-04-04 56 views
0

我使用這樣的結構:jQuery的ajaxStart

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

我想禁用每次用戶點擊它時提交按鈕。爲了使它通用 - 我想在ajaxStart方法中做到這一點,那麼有沒有辦法獲得初始化請求的按鈕ID? 謝謝

+0

你可以使用全局變量來保存按鈕的ID並在ajaxStart函數中使用它。 – 2012-04-04 05:59:31

+0

你可以將按鈕點擊事件傳遞給函數嗎? – Subodh 2012-04-04 06:01:12

+0

@sub_stantial:你是什麼意思?我應該通過按鈕點擊事件的功能? – nKognito 2012-04-04 06:03:54

回答

1

給你所有的提交按鈕的一類,例如:submitBtn,然後

$('.submitBtn').ajaxStart(function() { 
    $(this).prop('disabled', true); 
}).ajaxComplete(function() { 
    $(this).prop('disabled', false); 
}); 
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...'); 
}); 
+0

根據jQuery的手冊(http://api.jquery.com/ajaxStart/),回調函數沒有收到任何參數 – nKognito 2012-04-04 06:05:34

+0

我的錯誤,它應該是ajaxSend而不是ajaxStart。您可以將事件傳遞給ajaxSend,在這裏查看http://api.jquery.com/ajaxSend/ – Subodh 2012-04-04 06:17:32

+0

實際上,ajaxStart可以很好地處理參數,但其目標字段是指$('#wait')而不是初始按鈕 – nKognito 2012-04-04 06:20:35