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

How to find the row number in which button is clicked in jquery?

我有一个 table,其中最后一列中有一个名为问题的按钮。在每一行中,当我单击按钮时,我希望它调用一个 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

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

这可能会帮助你交配.. :)

注:Jquery版本>=1.7

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

仅供参考

index()

试试这个..(使用 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

在table行的按钮中添加一个class并绑定按钮如下图:-

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