2014-02-20 82 views
0

所以,我有ASP.NET WebForm的幾個按鈕,它引發了幾個AJAX方法,代碼後面的WebMethod類。捕獲任何按鈕的ID點擊

幾個按鈕有效地做同樣的thige,保存,刪除,更新等

有沒有辦法在全球範圍內捕獲的任何按鈕的射擊,抓住它的id,然後對其執行邏輯。

例如

<button id='button1'/> 
<button id='button2'/> 

$('button').click(function(e) { 
     e.preventDefault(); 
     alert(e); 
    }); 

這只是對象物體響應

編輯 要補充一點,這些按鈕在ASP.NET轉發器,因此讓ID作爲下面列出的產生如下:

buttonId_0

其中0增量取決於中繼器中呈現的項目。

編輯 因此,客戶端ID = 「靜」,整理出_0編號問題。

+0

'警報()'荷蘭國際集團的對象不給你任何幫助。改用'console.log(e)'。 –

回答

3

你需要this.id

$('button').click(function(e) { 
     e.preventDefault(); 
     alert(this.id);// this refers to the current element clicked 
}); 

閱讀this keywordthis

1

是的,你做到以下幾點。

<button id='button1'/> 
<button id='button2'/> 

$('button').click(function(e) { 
    e.preventDefault(); 
    alert(this.id); 
}); 
1

嘗試使用這樣的:

alert($(this).attr('id')); // get the id attribute..