如何在jquery中找到单击按钮的行号?

时间:2015-01-21 12:30:24

标签: javascript jquery ajax

我有一张表,其中最后一列中有一个名为issue的按钮。在每一行中,当我单击按钮时,我希望它调用一个从服务器获取特定值的ajax函数。服务器的值将根据所选的行而有所不同。我目前的代码如下

var $dntb = $("#dntb");
$dntb.on("focusin", function() {
        var i //have to get the row where the button is clicked 
        $("#iss" + i).click(function(event) {
        var sditmId = $("#sditm").val();
        var sdhedId = $("#sdhed").val();
        $.get('getstock', {
               sditmId: sditmId,
               sdhedId: sdhedId
        }, function(response) {
        $("#stk").val(response);
        });
        });
});

请告诉我如何找到' i'的价值。在上面的代码中,如果这是错误的请告诉我正确的方法

表格如下:

<table class="table table-bordered table-striped table-hover" id="dntb">
                        <thead>
                        <th><strong>Sales Order Number</strong></th>
                        <th><strong>Reference Order</strong></th>
                        <th><strong>Order Item</strong></th>
                        <th><strong>Quantity</strong></th>
                        <th><strong>Transport Time</strong></th>
                        <th><strong>Mode of Shipment</strong></th>
                        <th><strong>Delivery Date</strong></th>
                        <th><strong>Actions</strong></th>
                        </thead>
                        <jsp:useBean id="gen" scope="request" class="Common.General"/>
                        <tbody>
                            <c:set var="i" value="0"/>
                            <c:forEach items="${dlv.allDeliveryDetails}" var="row">
                                <tr>
                                    <td>${row.sdhedIdDn}</td>
                                    <td>${row.sdhedDn}</td>
                                    <td>${row.sditmDn} - ${row.prdDn}</td>
                                    <td>${row.qtyDn}</td>
                                    <td>${row.trntmDn}</td>
                                    <td>${row.mshDn}</td>
                                    <td>${row.datDn}</td>
                                    <td>
                                        <button type="button" class="btn btn-default btn-sm btn-primary" data-toggle="modal" data-target="#myModal" onclick="setData('${row.sdconDn}', '${row.sdhedIdDn}', '${row.sditmDn}', '${row.sdconIdDn}')" id="iss">
                                            <span class="glyphicon glyphicon-download"></span> Issue
                                        </button>
                                    </td>
                                </tr>
                                <c:set var="i" value="${i+1}"/>
                            </c:forEach>
                        </tbody>
                        <tfoot>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                        </tfoot>
                    </table>

3 个答案:

答案 0 :(得分:4)

这可能有助于你交配...... :)

注意:Jquery版本&gt; = 1.7

$('body').on('click', 'button', function(event) {
        var rowindex = $(this).closest('tr').index();
        console.debug('Index', rowindex);
    });

<强> FYI

index()

答案 1 :(得分:0)

试试这个..(使用class而不是id,因为id是唯一的)

$('table').on('click','button',function(){
    $( this ).closest('tr').find('.field1').val('bind value for field one.!');
    $( this ).closest('tr').find('.field2').val('bind value for field two.!');
});
你也可以用

$('.button').click(function(){
    $( this ).closest('tr').find('.field1').val('bind value for field one.!');
    $( this ).closest('tr').find('.field2').val('bind value for field two.!');
});

但如果您的DOM在加载页面后插入,则点击方法将无效。

SEE DEMO

答案 2 :(得分:0)

在表格行的按钮中添加一个类并绑定按钮,如下所示: -

&#13;
&#13;
$(".btnClass").click(function() {
  $(this).closest("tr");
  // anything you want to do
  return false;
});
&#13;
&#13;
&#13;