admin管理员组

文章数量:1430536

I am trying to extract the td values belonging a td rowspan.

<!DOCTYPE html>
<html>
<body>
<table border="1">
    <tr>
        <th>Month</th>
        <th>Expenses</th>
        <th>Exp/ Divided</th>
    </tr>
    <tr>
        <td rowspan="2">January</td>
        <td rowspan="2">$100</td>
        <td>$50</td>
    </tr>
    <tr>
        <td>$50</td>
    </tr>
    <tr>
        <td rowspan="3">February</td>
        <td rowspan="3">$400</td>
        <td>$150</td>
    </tr>
    <tr>
        <td>$150</td>
    </tr>
    <tr>
        <td>$100</td>
        <tr></tr>
</table>
</body>
</html>

So if I click on January for example I have to extract its divided expenses: $50, $50

if I click on February I have to extract its divided expenses: $150, $150, $100

I am trying to extract the td values belonging a td rowspan.

<!DOCTYPE html>
<html>
<body>
<table border="1">
    <tr>
        <th>Month</th>
        <th>Expenses</th>
        <th>Exp/ Divided</th>
    </tr>
    <tr>
        <td rowspan="2">January</td>
        <td rowspan="2">$100</td>
        <td>$50</td>
    </tr>
    <tr>
        <td>$50</td>
    </tr>
    <tr>
        <td rowspan="3">February</td>
        <td rowspan="3">$400</td>
        <td>$150</td>
    </tr>
    <tr>
        <td>$150</td>
    </tr>
    <tr>
        <td>$100</td>
        <tr></tr>
</table>
</body>
</html>

So if I click on January for example I have to extract its divided expenses: $50, $50

if I click on February I have to extract its divided expenses: $150, $150, $100

Share Improve this question edited Jan 27, 2014 at 6:18 Rohan Kumar 40.6k11 gold badges80 silver badges110 bronze badges asked Jan 27, 2014 at 6:11 Andy ChavesAndy Chaves 1472 gold badges2 silver badges10 bronze badges 1
  • 2 to be honest with you, i am plsql programmer, and don´t know this code , I just start reading some books, but my team ask me to plete this task by using JavaScript, but I am getting there, not yet, for now I just can ask for some help and learn – Andy Chaves Commented Jan 27, 2014 at 6:21
Add a ment  | 

2 Answers 2

Reset to default 3

Try this,

$(function(){
    $('.rowspan').on('click',function(){
        $tr=$(this).closest('tr');
        rowspan=$(this).attr('rowspan');
        index=$('tr').index($tr);
        tot=parseInt(index)+parseInt(rowspan);
        for(var i=index,len=tot;i<len;i++){
            console.log($('tr:eq('+i+') td:last').text());
        }
    });
});

Working Demo

With jquery you can simply select the objects like so:

$("td[rowspan='2']");

To get the containing values it would be:

$("td[rowspan='2']").text();

More on this: http://api.jquery./attribute-equals-selector/

本文标签: javascriptHow to get Rowspan Rows Values with JS or jQueryStack Overflow