最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get HTML table row data, based on checkbox selection in each row? - Stack Overflow

programmeradmin1浏览0评论

I have a table dynamically created with java script.It has one checkbox in each row as the first column.I want to fetch the row data based on the checkboxes selected of respective rows.

var table = document.getElementById("myTable");
var row = table.insertRow(0);
var cell0 = row.insertCell(0);
var cell1 = row.insertCell(1);

cell0.innerHTML = 'Select'; 
cell1.innerHTML = 'Epic';
cell0.innerHTML = " checkbox html code ";
cell1.innerHTML = epicSeries[j];

Actually too many columns are there I am putting just two of them. I have lot of epics down the column header 'epic' and one checkbox as the first column in each row.I want row data based on checkbox selcted.

Sorry code was too long so I cant paste all of them.

I have a table dynamically created with java script.It has one checkbox in each row as the first column.I want to fetch the row data based on the checkboxes selected of respective rows.

var table = document.getElementById("myTable");
var row = table.insertRow(0);
var cell0 = row.insertCell(0);
var cell1 = row.insertCell(1);

cell0.innerHTML = 'Select'; 
cell1.innerHTML = 'Epic';
cell0.innerHTML = " checkbox html code ";
cell1.innerHTML = epicSeries[j];

Actually too many columns are there I am putting just two of them. I have lot of epics down the column header 'epic' and one checkbox as the first column in each row.I want row data based on checkbox selcted.

Sorry code was too long so I cant paste all of them.

Share Improve this question edited Oct 11, 2012 at 13:17 Yiğit Yener 5,9862 gold badges24 silver badges26 bronze badges asked Oct 11, 2012 at 12:46 ravikantravikant 671 gold badge1 silver badge11 bronze badges 2
  • post some codes where you've created the checkbox and the javascript function. It would be helpful – polin Commented Oct 11, 2012 at 12:49
  • Will this fecth be called separately or on every change of any of the checkboxes? – David Mårtensson Commented Oct 11, 2012 at 12:59
Add a ment  | 

3 Answers 3

Reset to default 2

Having now an example of your code and bit more clear requirement, i think you should do the folowing:

$('#myTable input[type=checkbox]:checked').each(function() { 

   var row = $(this).parent().parent();
   var rowcells = row.find('td');
   // rowcells contains all td's in the row
   // you can do 
   // rowcells.each(function() {var tdhtml = $(this).html(); }); 
   // to cycle all of them    

});

If you have table like that:

<table>
  <tr>
     ....
     <td><input type="checkbox" name="cb1" checked></td>
     ...   
  </tr>
</table>

This code will return all <tr>'s with checked checkboxes

If row selecting check box is in a deeper level you should as more .parent()'s as needed

This exemple uses jQuery of course.

$('table#tableid input[type=checkbox]').each(function() {
   if ($(this).is(':checked')) {
     .... 
   }
});

something like that i supose

This is what I used in my case using jquery:

$('.chkbox').click(function(){
    var row = jQuery(this).closest('tr');//your nearest row for the check box

    $(row).each(function(){
        //get all data using the id and use/store it
        $(this).find(".item").html();
    });
});

For each checkbox and for each item in a row give a class(I used chkbox for all checkboxes and item, price etc. for all items of a single row)

发布评论

评论列表(0)

  1. 暂无评论