简体   繁体   English

如何使用带有类的Jquery从动态创建的复选框中获取值

[英]How to get the value from dynamically created checkbox using Jquery with class

I am trying to get the value from checkbox when checked which is created dynamically with jquery associated with html table am using class to get the value but am unable to get it 我正在尝试从复选框中获取值,而该复选框是使用与html表关联的jquery动态创建的,因此正在使用类获取该值,但无法获取它

My code is like this 我的代码是这样的

Input created with 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 to get the value jQuery获取价值

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

Please help me how to fix this. 请帮助我解决此问题。

Thanks in advance 提前致谢

Working Fiddle 工作小提琴

Try: 尝试:

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

Use 'change' event: 使用“更改”事件:

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

Instead of creating it like that use Create element and add id dynamically then use your function I guess it will work, actually worked in my case 与其像使用Create元素那样创建它并动态添加id然后使用您的函数,不如创建它,我想它会起作用,在我的情况下实际上是起作用的

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

You can do using following for dynamically created elements: 您可以对动态创建的元素使用以下命令:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM