2015-04-28 46 views
0

我使用帶複選框的引導表。 html呈現如下:按名稱選擇動態複選框... jQuery

<td class="bs-checkbox"> 
<input data-index="0" name="btSelectItem" type="checkbox"> 
</td> 
<td class="bs-checkbox"> 
<input data-index="1" name="btSelectItem" type="checkbox"> 
</td> 

當用戶選中其中一個框時,我想要一個事件觸發來捕獲它。這就是我試圖這樣做,無濟於事:

 $('table').on('click', 'input[name=btSelectItem]', function (index, obj){ 
    alert(obj); 
}); 

數據來自ajax調用,所以它的動態。當用戶點擊其中一個複選框時,如何激活我的代碼?由於

+1

如果'table'是動態的一樣,那麼你應該設置另一個元素開始。 –

+0

什麼是obj?你想在那裏訪問什麼? –

+0

@Chaitanya Gadkari - obj只是虛擬數據。當我點擊一個複選框時,我會希望彈出一個警告框。它不 – BoundForGlory

回答

1

你要正確的方式,如u_mulder建議,如果表是動態的一樣,用另一種元素,也許記錄

$(document).on('click', 'input[name=btSelectItem]', function(){ 
    alert($(this).attr('data-index')); // I suppose this is index you needed, i am not sure of obj 
}); 
+0

我必須做錯了什麼。這段代碼很簡單,應該可以工作。謝謝 – BoundForGlory

+0

我想只是變量傳遞給函數,我不知道如何準確地傳遞變量......但是你在'function(event)'中捕獲它並通過'event.Data'訪問數據 –