[英]How to get the row id onClick in jQuery/JavaScript?
I have a table populated with dynamic data.我有一个填充了动态数据的表。 the last column has a button which when I click I would like to pass the row id to the JS function.最后一列有一个按钮,当我单击该按钮时,我想将行 ID 传递给 JS function。 I need the id because I am doing a POST Ajax request and on success I want to take the response data and update the selected row with new data.我需要 id,因为我正在执行 POST Ajax 请求,成功后我想获取响应数据并使用新数据更新所选行。
This is what I would do to insert a new row:这就是我要插入新行的方法:
var rowNode = myTable.row.add([1,2,3,4,5,6,7,8]).draw();
but what can I do to get the row ID and update it with the response data?但是我能做些什么来获取行 ID 并使用响应数据更新它?
EDIT.编辑。 Datatable:数据表:
<table id="myTable" class="display compact" cellspacing="0" width="100%">
<thead>
<tr>
<th>Name</th>
<th>Reg</th>
<th>Edit</th>
</tr>
</thead>
<tbody>
<?php foreach (get_all_customers_list() as $user) { ?>
<tr>
<td>
<b> <?php echo $user["recipient_name"]; ?></b>
</td>
<td>
<?php echo $user["registration_date"]; ?>
</td>
<td>
<button type="button" id="button_edit" onclick='edit_customer_request(<?php echo json_encode($user); ?>)' value="<?php echo $user; ?>" name="edit_customer">Editt</button>
</td>
</tr>
<?php }?>
</tbody>
</table>
Since you are only wanted to get the row id
of the clicked
row edit button.由于您只想获取clicked
的行编辑按钮的row id
。 You can simply use table.row
function and pass the actual tr
of the clicked button.您可以简单地使用table.row
function 并传递单击按钮的实际tr
。
Demo (Showing the actual id
(1, 2) which is stored as name)演示(显示存储为名称的actual id
(1, 2))
var table = $('#myTable').DataTable({}) //edit customer here function edit_customer_request(_this) { //Getting the actual table ID var row = $(_this).parents('tr')[0]; //Data table row id console.log(table.row(row).data()[0]); }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.21/js/jquery.dataTables.min.js" integrity="sha512-BkpSL20WETFylMrcirBahHfSnY++H2O1W+UnEEO4yNIl+jI2+zowyoGJpbtk6bx97fBXf++WJHSSK2MV4ghPcg==" crossorigin="anonymous"></script> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.21/css/jquery.dataTables.min.css" integrity="sha512-1k7mWiTNoyx2XtmI96o+hdjP8nn0f3Z2N4oF/9ZZRgijyV4omsKOXEnqL1gKQNPy2MTSP9rIEWGcH/CInulptA==" crossorigin="anonymous" /> <table id="myTable" class="display compact" cellspacing="0" width="100%"> <thead> <tr> <th>Name</th> <th>Reg</th> <th>Edit</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>Tiger Blah</td> <td><button type="button" class="button_edit" onclick='edit_customer_request(this, 1)' value="1" name="edit_customer">Edit</button></td> </tr>tr> <tr> <td>2</td> <td>Blah Nixon</td> <td><button type="button" class="button_edit" onclick='edit_customer_request(this,2)' value="2" name="edit_customer">Edit</button></td> </tr> </tbody> </table>
Demo (Showing the actual index of table - Index start from 0 to depending on how many rows you have)演示(显示表的实际索引 - 索引从 0 开始到取决于您拥有的行数)
var table = $('#myTable').DataTable({}) //edit customer here function edit_customer_request(_this) { //get the closest of clicked edit button var tr = $(_this).closest("tr"); //get the index of row var rowindex = tr.index(); //Index of row console.log(rowindex) }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.21/js/jquery.dataTables.min.js" integrity="sha512-BkpSL20WETFylMrcirBahHfSnY++H2O1W+UnEEO4yNIl+jI2+zowyoGJpbtk6bx97fBXf++WJHSSK2MV4ghPcg==" crossorigin="anonymous"></script> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.21/css/jquery.dataTables.min.css" integrity="sha512-1k7mWiTNoyx2XtmI96o+hdjP8nn0f3Z2N4oF/9ZZRgijyV4omsKOXEnqL1gKQNPy2MTSP9rIEWGcH/CInulptA==" crossorigin="anonymous" /> <table id="myTable" class="display compact" cellspacing="0" width="100%"> <thead> <tr> <th>Name</th> <th>Reg</th> <th>Edit</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>Tiger Blah</td> <td><button type="button" class="button_edit" onclick='edit_customer_request(this, 1)' value="1" name="edit_customer">Edit</button></td> </tr>tr> <tr> <td>2</td> <td>Blah Nixon</td> <td><button type="button" class="button_edit" onclick='edit_customer_request(this,2)' value="2" name="edit_customer">Edit</button></td> </tr> </tbody> </table>
this is my simple answer:这是我的简单回答:
var table = $('#table-values');
$('#table-values').on( 'click', 'tr', function(){
var id = this.id;
alert( 'Clicked row id '+id );
});
Easy.简单的。 :) :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.