javascript - How to get dynamically generated check box values - Stack Overflow

How to get the values of Dynamically generated CheckBoxes using jqueryfor(i=startAt;i<limit;i++){va

How to get the values of Dynamically generated CheckBoxes using jquery

for(i=startAt;i<limit;i++)
{
    var str = '<tr>'+
              '<td width="48" align="center"><input class="A" type="checkbox" name="checkbox" value='+ local[i]['_id'] +'></td>'+
              '<td width="270" >' + local[i]['_id'] +'</td>'+
              '<td width="883" class="alignRt">'+local[i]['count']+'</td>'+
              '</tr>'
            $("#tableBody").append(str);
}   

using this I am able to get the total checkboxes at runtime

$(document).on('click', '.A', function(){
var n = $("input:checked.A").length;
console.log(n)

But I want to get the values too.

How can I do that ?

How to get the values of Dynamically generated CheckBoxes using jquery

for(i=startAt;i<limit;i++)
{
    var str = '<tr>'+
              '<td width="48" align="center"><input class="A" type="checkbox" name="checkbox" value='+ local[i]['_id'] +'></td>'+
              '<td width="270" >' + local[i]['_id'] +'</td>'+
              '<td width="883" class="alignRt">'+local[i]['count']+'</td>'+
              '</tr>'
            $("#tableBody").append(str);
}   

using this I am able to get the total checkboxes at runtime

$(document).on('click', '.A', function(){
var n = $("input:checked.A").length;
console.log(n)

But I want to get the values too.

How can I do that ?

Share Improve this question edited Sep 23, 2015 at 19:51 user784540 asked Jun 13, 2013 at 13:21 Alok AgarwalAlok Agarwal 3,0993 gold badges25 silver badges34 bronze badges 5
  • 2 You are generating invalid HTML, ids must be unique – Quentin Commented Jun 13, 2013 at 13:22
  • 2 You should learn how to use the label element – Quentin Commented Jun 13, 2013 at 13:24
  • if i remove id than too i want to get the values of all generated checkboxes as soon as they get clicked @Quentin – Alok Agarwal Commented Jun 13, 2013 at 13:27
  • use .class instead. – Omar Commented Jun 13, 2013 at 13:27
  • @ Quentin i have updated the code i want the values at runtime – Alok Agarwal Commented Jun 13, 2013 at 13:30
Add a ment  | 

4 Answers 4

Reset to default 5

Use the :checked selector

jQuery('.A:checked')

You can then loop over the elements to get all their values.

$(document).on('click', '.A', function(){

    var n = $( "input:checked.A" ).length;
    var arr=[]
    for(i=0;i<n;++i){
        arr.push($($( "input:checked.A" )[i]).val())
    }
    alert(arr)
});

You can get checked checkboxes using:

$("#tableBody").find("input:checked");

It returns a list of checked checkboxes

use JQuery's $('#CheckboxID').val()

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745264001a4619340.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信