2011-09-15 30 views
1

我有一個存儲過程,將返回一個結果集與n數量的列(存儲過程的邏輯確定列返回),我用它們來建立一個「網格」在他們飛行。爲此,我使用我在運行時通過枚舉動態建立DataColumns一個DataTable:當我不知道按鈕名稱時,如何使多個按鈕調用相同的函數?

   <table> 
       <thead> 
        <% foreach (System.Data.DataColumn column in ((System.Data.DataTable)Model).Columns) 
         { %> 
        <th> 
         <%: column.Caption %> 
         <input id="Image1" type="image" src="../Content/theme/images/arrowhead-down.jpg" width="9px" height="5px"/></th> 
        </th> 
        <%}%> 
       </thead> 
       <tbody> 
        <% 
      foreach (System.Data.DataRow row in ((System.Data.DataTable)Model).Rows) 
      {%> 
        <tr> 
         <% foreach (var column in row.ItemArray) 
          { %> 
         <td> 
          <%: column.ToString() %> 
         </td> 
         <%}%> 
        </tr> 
        <% 
      } 
        %> 
       </tbody> 
      </table> 

型圖像(按鈕)的輸入將被包括爲每個新的列元素。從本質上講該按鈕將允許列使用一些腳本來隱藏:

$(document).ready(function() { 
      $('#Image1').click(function() { 
       $('td:nth-child(3),th:nth-child(3)').hide(); 
      }); 
     }); 

所以我的問題是這樣的,我怎麼可以在每個新創建的按鈕,無論調用相同的功能按鈕的名稱(因爲我不知道將會創建多少個按鈕或他們的名字)?

回答

2

給他們所有相同的CSS類名稱。在使用.NET時避免將JavaScript連接到ID,它喜歡爲自己提供它們。

然後你可以使用:

$('.buttonClassOfYourChoice').click(function() { 
     ....common click code... 
}) 

「這」 指的是被點擊的按鈕:

$('.buttonClassOfYourChoice').click(function() { 
    alert($(this).html()) 
}) 
+0

這完美地工作!像你這樣的人爲什麼我不斷回到這個網站,謝謝先生! – northpole

相關問題