2011-09-28 106 views
0

當按鈕點擊這個函數被調用防止功能的執行從雙擊

save_edit: function() { 

    this.do_save(); 

}, 

我試圖禁用按鈕,一旦它被點擊,以防止雙擊。我如何在這個功能中做到這一點?

+0

怎麼叫那個函數? –

+0

你的意思是禁用任何subequent點擊?或禁用「雙擊」? –

+0

當按鈕點擊從所有的聯合國執行這個樂趣將執行第一.....................和禁用次級聯繫 –

回答

0
$("#el").click(function() { 
    $(this).prop('disabled',true); 
    // do more stuff 
    $(this).prop('disabled',false); // if desired 
}); 

或者,如果你有幾個按鈕:

$(":button").click(function() { 
    $(this).prop('disabled',true); 
    // $(this) is a jQuery object of the button that was just clicked 
    str_id = $(this).attr('id'); // if you really need the ID itself 
    // do more stuff 
}); 
+0

上的dblclick在舊版本的jquery(<1:6)中,你將需要使用attr而不是prop。 – realshadow

+0

我怎麼能從這個函數中得到那個按鈕的id ??????? –

+0

這是真的;如果使用舊版本的jQuery,請使用'$(this).attr('disabled','disabled')'和$(this).attr('disabled','')'。 – Blazemonger

0
$("#element").dblclick(function(){ 
    return false; 
}); 

$("#element").click(function(){ 
    //do stuff 
}); 
+0

如何我可以得到該按鈕的ID來自這個功能??????? –

+0

@TurkeshPatel:它取決於你想要禁用的內容在 – genesis

0

如果你不想禁用按鈕,可以從調用函數的時間設定peroid停止方法。

save_edit: function() { 
    if(this.timer)return; 
    this.do_save(); 
    this.timer = window.setTimeout(function(){}, 300); //number of ms before you want another submit to happen 
}, 
+0

日Thnx您的解決方案,但它不適合我 –

+0

辦法犯規機制保障的這種風格防止do_save被調用兩次,想象一個異步調用調用.save_edit()的回調,它可以並行發生......你」再次「鎖定」 do_save開始後,你需要在它之前鎖定或你有一個競爭條件。 –

0
$("#btn").one(function(){ 
    //do stuff once and remove handler 
}); 

$("#btn").dblclick(function(){ 
    return false; 
});