簡體   English   中英

如何找到在jQuery中單擊按鈕的行號?

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

我有一個表,其中最后一列中有一個稱為問題的按鈕。 在每一行中,當我單擊按鈕時,我希望它調用一個從服務器獲取特定值的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>

這可能有助於您交配.. :)

注意:jQuery版本> = 1.7

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

費耶

指數()

試試看..(使用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在加載頁面后插入,則click方法將不起作用。

查看演示

將一個類添加到表行中的按鈕並綁定按鈕,如下所示:-

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM