2014-07-02 164 views
0

我添加動態超鏈接,並希望執行點擊事件時,如果用戶點擊它,但它不會啓動。在表中點擊動態添加超鏈接點擊

<table id="assinged_areas" class="table table-bordered table-condensed table-condensed"> 
      <tbody></tbody> 
     </table> 

當用戶選擇在下拉一些選項,然後它去,並從服務器領域則AJAX通過將數據傳遞給這個函數

function updateAssignedAreas(data) { 

      debugger; 
      var areas = data.split(','); 
      $.each(areas, function (i, item) { 
       $('#assinged_areas > tbody:last').append('<tr><td>' + item + '</td><td>' + '<a class=\'btn btn-danger\'><i class=\'fa fa-minus-circle\'></i></a>' 
        + '</td></tr>'); 
      }); 
     } 

這是取消分配,當用戶點擊該功能得到調用這個函數必須調用但在所有

$('.btn-danger').click(function (parameters) { 
      debugger; 
     }); 

回答

1

使用event delegation動態元素

不點火
$("#assinged_areas").on("click", '.btn-danger', function (parameters) { 
    var row=$(this).closest("tr"); 
    alert($(this).closest("tr").find("td").eq(0).html()); //will get the inner text of first td 
}); 

事件代理允許您將單個事件偵聽器附加到父元素,該子元素將爲所有匹配選擇器的子元素觸發,無論這些子元素是現在存在還是將來添加。

+1

作爲Anoop說,http://learn.jquery.com/events/event-delegation/ –

+0

嗨,謝謝你的答案,我怎樣才能得到單擊按鈕的行細節 –

+0

@ineffablep你可以分享最後呈現的HTML? –