获取表格行的值?

时间:2014-03-21 10:45:01

标签: jquery

以下是代码:

html

<table class="table table-striped table-bordered table-condensed">
<thead>
    <tr style='background-color: white'>
        <th> <i class="fa fa-male"></i> Name</th>
        <th><i class="fa fa-bars"></i> Particular</th>
        <th><i class="fa fa-envelope-o"></i>Unit</th>
        <th><i class="fa fa-map-marker"></i>Quantity</th>
        <th><i class="fa fa-phone"></i>From</th>
        <th><i class="fa fa-phone"></i>Date</th>
        <th><i class="fa fa-phone"></i>TAKE actions</th>
    </tr>
</thead>
<tbody>
    <tr>
        <td>sam</td>
        <td>dsfjhfskdj</td>
        <td>jhg</td>
        <td>9</td>
        <td>Snehp</td>
        <td>2014-03-21 08:55:59 UTC
            <td>
                <form action="/requisitions/1" class="button_to" method="post">
                    <div>
                        <input name="_method" type="hidden" value="delete" />
                        <input class="btn btn-danger" data-confirm="Are you sure you want to delete?" type="submit" value="Remove" />
                        <input name="authenticity_token" type="hidden" value="LRKQigwpQMhvPZxJEImpmQzsEvC8/Sxd/PIqi2r5Hxs=" />
                    </div>
                </form>
                <form action="/po" class="button_to" method="get">
                    <div>
                        <input class="btn btn-info" data-confirm="Really wants to generate Po?" id="po" type="submit" value="PO" />
                    </div>
                </form>
                <form action="/capax" class="button_to" method="get">
                    <div>
                        <input class="btn btn-warning" data-confirm="Are you sure?" type="submit" value="Capax" />
                    </div>
                </form>
            </td>
            <tr>
                <tr>
                    <td>mpobile</td>
                    <td>rhyhjfryhn</td>
                    <td>dh</td>
                    <td>5</td>
                    <td>Snehp</td>
                    <td>2014-03-21 09:48:01 UTC
                        <td>
                            <form action="/requisitions/2" class="button_to" method="post">
                                <div>
                                    <input name="_method" type="hidden" value="delete" />
                                    <input class="btn btn-danger" data-confirm="Are you sure you want to delete?" type="submit" value="Remove" />
                                    <input name="authenticity_token" type="hidden" value="LRKQigwpQMhvPZxJEImpmQzsEvC8/Sxd/PIqi2r5Hxs=" />
                                </div>
                            </form>
                            <form action="/po" class="button_to" method="get">
                                <div>
                                    <input class="btn btn-info" data-confirm="Really wants to generate Po?" id="po" type="submit" value="PO" />
                                </div>
                            </form>
                            <form action="/capax" class="button_to" method="get">
                                <div>
                                    <input class="btn btn-warning" data-confirm="Are you sure?" type="submit" value="Capax" />
                                </div>
                            </form>
                        </td>
                        <tr>
                            <tr>
                                <td>sssssss</td>
                                <td>sneh pandy</td>
                                <td>dkfgh</td>
                                <td>8</td>
                                <td>Snehp</td>
                                <td>2014-03-21 10:01:52 UTC
                                    <td>
                                        <form action="/requisitions/3" class="button_to" method="post">
                                            <div>
                                                <input name="_method" type="hidden" value="delete" />
                                                <input class="btn btn-danger" data-confirm="Are you sure you want to delete?" type="submit" value="Remove" />
                                                <input name="authenticity_token" type="hidden" value="LRKQigwpQMhvPZxJEImpmQzsEvC8/Sxd/PIqi2r5Hxs=" />
                                            </div>
                                        </form>
                                        <form action="/po" class="button_to" method="get">
                                            <div>
                                                <input class="btn btn-info" data-confirm="Really wants to generate Po?" id="po" type="submit" value="PO" />
                                            </div>
                                        </form>
                                        <form action="/capax" class="button_to" method="get">
                                            <div>
                                                <input class="btn btn-warning" data-confirm="Are you sure?" type="submit" value="Capax" />
                                            </div>
                                        </form>
                                    </td>
                                    <tr>
</tbody>
</table>

Jquery

$(function(){

        $('#po').click(function(){


                var $row = $(this).parents('tr'); 
                alert($row.find('td:eq(0)').html());
                alert($row.find('td:eq(1)').html());
                alert($row.find('td:eq(2)').html());
                alert($row.find('td:eq(3)').html());
                alert($row.find('td:eq(4)').html());

        });
    });

问题:

当我点击po button时,只有它适用于第一行,它不适用于任何其他行吗?

FIDDLE

1 个答案:

答案 0 :(得分:0)

您有重复的ID, ID应该是唯一的,您可以使用类来代替 -

另外,由于你有嵌套表,你需要使用最接近的表 -

var $row = $(this).closest('tr');