2014-09-30 27 views
0

您可以看看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); 
}); 

感謝

回答

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);  

}); 
0

如果我正確理解你,你可以嘗試這樣的事:

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

更新fiddle here