2013-11-25 40 views
0

我很新的jQuery和我在jQuery中從按鈕生成一個ItemTemplate在ASP轉發器中生成的按鈕點擊事件的問題。ASP轉發器 - 抓住生成的按鈕點擊jQuery功能

我花了幾個小時尋找一個可以工作的答案,但沒有運氣。

一個從我的.aspx頁面中剪斷:

<asp:Repeater ID="rptJobs" runat="server"> 
    <ItemTemplate> 
     <asp:Panel ID="Panel1" runat="server"> 
      <table> 
       <tr> 
        <td>Desc:</td> 
        <td></td> 
        <td><%#Eval("DESCRIPTION")%></td> 
       </tr> 
      </table> 
      <asp:Button runat="server" ID="myBtn" Tag='<%# Eval("JOB_NO") %>' Text='Go' /> 
     </asp:Panel> 
    </ItemTemplate> 
</asp:Repeater> 

生成的HTML:

<div id="rptJobs_ctl00_Panel1"> 
    <table> 
     <tr> 
      <td>Desc:</td> 
      <td></td> 
      <td>Test Data 1</td> 
     </tr> 
    </table> 
    <input type="submit" name="rptJobs$ctl00$myBtn" value="Go" id="rptJobs_ctl00_myBtn_0" Tag="MI0683" /> 
</div> 
<div id="rptJobs_ctl01_Panel1"> 
    <table> 
     <tr> 
      <td>Desc:</td> 
      <td></td> 
      <td>Test Data 2</td> 
     </tr> 
    </table> 
    <input type="submit" name="rptJobs$ctl01$myBtn" value="Go" id="rptJobs_ctl01_myBtn_1" Tag="MI0684" /> 
</div> 

現在,這個工程的jQuery(指定產生的按鈕ID):

<script language="javascript" type="text/javascript"> 
    $(document).ready(function() { 
     $(function() { 
      $("#rptJobs_ctl00_myBtn_0").click(function() { 
     // This displays the text from the Tag element of the button...  
       alert($(this).attr("tag")); 
      }); 
     }); 

    }); 
</script> 

不過,我希望能夠使用將爲所有生成的bu調用click事件的泛型函數ttons - 任何幫助表示讚賞!

特德^ h

+1

我會建議給他們一個普通的css類,這樣你就可以使用jQuery中的一個類選擇器來定位所有不依賴於它的id的按鈕。最後添加另一個屬性(或另一個類,但它不是乾淨的)來標識單擊的「行」,如果你不想分析這個id。 –

+0

嘗試從後面的代碼中獲取標籤屬性。它可能更好地工作,沒有jQuery的干擾。 – thenewseattle

+0

感謝Bartdude指引我在正確的方向。 我對.ASPX文件(添加一個類)的修改後的按鈕聲明: 'Text ='<%#Eval(「JOB_NO」)%>'ClientIDMode ='Predictable「/> 我的jQuery功能: $(」。xxx「)。click(function(){alert 'this'); var t = $(this).attr(「tag」); alert(t); return false; }); This Works!謝謝。 –

回答

0

注意,所有的名字與myBtn(您設置ID的值)結束。

<script language="javascript" type="text/javascript"> 
    $(document).ready(function() { 
     $("[name$='myBtn']").click(function() { 
      alert($(this).attr("tag")); 
      return false; 
     }); 
    }); 
</script> 

...或限制選擇僅輸入:在您選擇項目名稱與在myBtn結束所以使用jQuery選擇$("input[name$='myBtn']").click(function() { ...

閱讀:jQuery Selectors

UPDATE :正如Bartdude所說,在課堂上選擇其實更快。這裏有一些結果迭代利用類似於測試1000次1000次轉發項目this SO answer

選擇器用於:

by Class: $(".fakeClass") 
by Name Limited to Input: $("input[name$='myBtn']") 
by Name: $("[name$='myBtn']") 
by Data Attr: $('[data-fakeAttr="me"]') 
by ID Limited to Input: $("input[id*=myBtn]") 
by ID: $("[id*=myBtn]") 

鉻:

by Class: 281 
by Name Limited to Input: 655 
by Name: 687 
by Data Attr: 515 
by ID Limited to Input: 702 
by ID: 858 

IE 9:

by Class: 367 
by Name Limited to Input: 1711 
by Name: 3257 
by Data Attr: 3779 
by ID Limited to Input: 1663 
by ID: 3695 

有些舊的瀏覽器,Safari瀏覽器5:

by Class: 793 
by Name Limited to Input: 925 
by Name: 1023 
by Data Attr: 983 
by ID Limited to Input: 1005 
by ID: 1242 

即使舊的瀏覽器,火狐3.6:

by Class: 1320 
by Name Limited to Input: 2557 
by Name: 2556 
by Data Attr: 2272 
by ID Limited to Input: 2726 
by ID: 5458 
0
$("[id*=myBtn]").click(function() { 
     // This displays the text from the Tag element of the button...  
       alert($(this).attr("tag")); 
      }); 

或者你可以寫

myBtn.Attributes.Add("onClick", "return JavascriptMethod('" + ID.ToString() + "');"); 
ItemDataBound事件。

in .cs文件。