2016-12-30 51 views
1

我想從複選框中獲取值,當檢查它是與jQuery相關聯的HTML表動態創建的我正在使用類來獲取值,但無法獲取它如何從動態創建的複選框獲取值使用Jquery與類

我的代碼是一樣的jQuery

"<td><div class=" + "checkbox checkbox-primary" + "><input type=" + "checkbox" + " class=" + "cbCheck" + " value=" + "" + data.d[i].Rowname + "" + "" + data.d[i].one + "" + "></div></td>" 

創造了這個

輸入jquery得到值

$("#table").on(":checked", ".cbCheck", function() { 
    var id = $(this).attr("value"); 
    alert(id); 
}); 

請幫我解決這個問題。

在此先感謝

回答

2

Working Fiddle

嘗試:

$('table tr td').on('click','.cbCheck',function() { 
    if ($(this).is(':checked')) { 
    alert($(this).attr('id')) 
    } 
    else 
    alert('unchecked'); 
}); 
+0

我想這樣的$(document)。就緒(函數(){ $( 「#表」)上(「點擊。 「,」.cbCheck「,函數(){var_id = $(this).attr(」id「); alert(id); }); }); – Raviteja

+0

工作很好的答案 – Raviteja

+0

很高興我能幫上忙。 –

0

使用「變」事件:

$("#table").on("change", ".cbCheck", function() { 
    var id = $(this).attr("value"); 
    alert(id); 
}); 
0

而是像 使用創造它創建元素,並添加ID動態然後用你的功能我想它會工作的,實際上在我的情況下工作

var newCheckBox = document.createElement('input'); 
newCheckBox.type = 'checkbox'; 
newCheckBox.id = 'ptworkinfo'; 
0

您可以使用以下動態創建的元素S:

$(document).on("click", ".cbCheck", function() { 
    var value=$(this).attr("value"); 
    alert(value); 
}); 
相關問題