Javascript 如何查找在jquery中单击按钮的行号?
我有一个表,其中最后一列中有一个名为issue的按钮。在每一行中,当我单击按钮时,我希望它调用一个从服务器获取特定值的ajax函数。来自服务器的值将根据所选行的不同而变化。我目前的代码如下Javascript 如何查找在jquery中单击按钮的行号?,javascript,jquery,ajax,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(funct
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);
});
供参考
这可能有助于你的配偶 注: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,则单击方法将不起作用
试试这个。。使用类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,则单击方法将不起作用
向表行中的按钮添加一个类并绑定按钮,如下所示:- $.btnClass.clickfunction{ $this.closesttr; //你想做什么 返回false;
}; 向表行中的按钮添加一个类并绑定按钮,如下所示:- $.btnClass.clickfunction{ $this.closesttr; //你想做什么 返回false;
};在focusin事件中嵌套click事件是一个非常糟糕的想法在focusin事件中嵌套click事件是一个非常糟糕的想法更好的方法是将其委托给表级,而不是主体IMHOB,更好的方法是将其委托给表级,而不是主体imho