2014-03-07 26 views
0

這裏按鈕在循環中創建意味着國家和按鈕的數量將相同。 現在我想單擊每個按鈕的事件按其ID和各自的input textboxes的ID。在循環中創建的jQuery中的多個按鈕單擊事件

@foreach (var item in ViewBag.countries) { 
    var textId = item.Name+"_text";    
    var cnt = item.Name;    
    <td><label class="control-label" >@item.Name</label></td>            
    <td>@Html.EditorFor(model => model.Amount)</td> 
    <td>@Html.HiddenFor(model => model.CountryId, new { @Value = item.ID })</td> 
    <td><input type="text" [email protected] /></td>    
    <td><button type="button" class="btn-primary" [email protected]>Update</button></td>    
} 
+0

這不像jquery..plz標記你的框架,以便人們可以理解 – kamesh

回答

3

不知道如果我跟隨,但爲了獲得點擊處理程序中的ID,你可以做到這一點

$('.btn-primary').click(function(){ 
    var buttonId = this.id; 
    // the rest of the code 
}) 
+0

還有一件事..如果id是「美國」,那麼它只顯示「美國」。請你幫忙,我可以得到整個字符串。 – user3206357

+0

帶空格的ID無效,您需要用(例如)「_」替換空格。 –

+0

好的......謝謝.. :) – user3206357

2

要使用動態元素上

$('.btn-primary').on('click',function(){ 
    var Id = $(this).attr("id"); 
}) 
相關問題