从行中的复选框获取表值的问题

时间:2014-09-30 15:14:32

标签: javascript jquery

请您查看this demo并告诉我如何在Js数组或对象中存储(推送)已检查行的值?

var obj=[
    {
        id : "001",
        name : "apple",
        category : "fruit",
        color : "red"
    },
    {
        id : "002",
        name : "melon",
        category : "fruit",
        color : "green"
    },
    {
        id : "003",
        name : "banana",
        category : "fruit",
        color : "yellow"
    }
]
$.each(obj, function (index, item) {
     var eachrow = "<tr>"
                 + "<td id="+index+"><input type='checkbox'></td>"
                 + "<td>" + item["id"] + "</td>"
                 + "<td>" + item["name"] + "</td>"
                 + "<td>" + item["category"] + "</td>"
                 + "<td>" + item["color"] + "</td>"
                 + "</tr>";
     $('#tbody').append(eachrow);
});

由于

2 个答案:

答案 0 :(得分:0)

如果我理解正确,你可以尝试这样的事情:

$(function(){
  $('input[type="checkbox"]').on('change', function(){
    data.push($(this).parent().attr('id'));
  });
});

更新了fiddle here

答案 1 :(得分:0)

试试这个,

Live demo

单击“获取行”按钮后,您将获得所需的输出。

$("#btn").on("click",function(){

    var checkedRows = [];
    $("#tbody tr").each(function(){

        if($(this).find("input").is(":checked")){
            checkedRows.push($(this).find("td:eq(1)").html());
        }    
        console.log(checkedRows);    
    });

    var result = [];

    $.each(obj,function(item){
        if(checkedRows.indexOf(item.id)>-1)
       result.push(item) ;
    });   

      console.log(result);    

});