简体   繁体   English

使用 Jquery/Javascript 根据按钮单击获取表行值

[英]Get table row value based on button click using Jquery/Javascript

I need to get the table row value from onclick event.我需要从 onclick 事件中获取表格行值。 Below the table, I am using在表格下方,我正在使用

Group name|Manage group

Test 1    | Button

Test 2    | Button

If I click 'Button' from the table row, I need to get the respective group name value from the table row.如果我从表格行中单击“按钮”,我需要从表格行中获取相应的组名值。

Below the script, I am trying.在脚本下面,我正在尝试。 But it is printing only the first group name value "Test 1" for both buttons.但它只打印两个按钮的第一个组名称值“Test 1”。

var table = document.getElementById("user_table");
       var rows = table.rows;
       for (var i = 1; i < rows.length; i++) {
           rows[i].onclick = (function (e) {
               var rowid = (this.cells[0].innerHTML);
               var j = 0;
               var td = e.target;
               while( (td = td.previousElementSibling) != null ) 
                   j++;
               alert(rows[1].cells[j].innerHTML);
           });
       }

alert(rows[1].cells[j].innerHTML) printing group name value Test 1 and if I click second row button it is showing Test 1 only.But I need to get Test 2 if I click second row button. alert(rows[1].cells[j].innerHTML) 打印组名值 Test 1,如果我点击第二行按钮,它只显示 Test 1。但是如果我点击第二行按钮,我需要获取 Test 2。

Html file Html 文件

<table id="user_table"  style='overflow-x:scroll;overflow-y:scroll;width:100%;height:auto' class="table table-bordered" >
 <thead>
 <tr class="info">
   <th>Group Name</th>
   <th>User Group</th>

 </tr>
 </thead>
 <tbody id="table_body">
 <br>
 
   <% @array.each do |user| %>
   <tr>
   
     <td >
       <%=user['group_name']%>
     </td>
       <td>
         <button id='manageusrbtn' name="button" type="button" class="editbtn" onclick="manageUserGroups()" style="background-color: #008CBA; color: white; border-radius: 6px;" >Manage User Groups</button>
       </td>
        
     <% end %>
   </tr>
 </tbody>
 </table>

I would suggest to attach the click event handler to the button and not to each cell.我建议将单击事件处理程序附加到按钮而不是每个单元格。 For this you can use:为此,您可以使用:

The snippet:片段:

 var table = document.querySelectorAll('#user_table tr button'); table.forEach(function(ele) { ele.addEventListener('click', function (e) { var rowid = (this.closest('td').previousElementSibling.innerHTML); console.log(rowid); }) });
 <table id="user_table"> <tr> <th>Group name</th> <th>Manage group</th> </tr> <tbody> <tr> <td>Test 1</td> <td><button>Button</button></td> </tr> <tr> <td>Test 2</td> <td><button>Button</button></td> </tr> </tbody> </table>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript / jquery在表中单击按钮获取行ID - Javascript/jquery Get Row Id on button click in a table 通过鼠标单击获取表格行的值? (jQuery) - Get value of Table Row with mouse click? (JQuery) jQuery / JavaScript如何获取单击它的按钮的值,每行按钮只能激活一个按钮 - jQuery/JavaScript how to get value of button that click on it, each row of buttons can only active only one button jQuery-从按钮单击事件中获取具有包含特定值的字段的行的表头值 - Jquery - get table header value from a row that has a field with a specific value, from a button click event 如何在行单击时使用 jquery 获取表格单元格值并将值显示到另一个表格? - How to get table cell value using jquery on row click and show the value to another table? 单击一个按钮并使用JavaScript(或jquery)获取同一行中单元格的值 - Click a button and get values of cells in same row with JavaScript (or jquery) 单击html表中的按钮并获取同一行的值 - click a button inside html table and get the value on the same row "单击 HTML 表格并获取行号(使用 Javascript,而不是 jQuery)" - Click on HTML table and get row number (with Javascript, not jQuery) 单击按钮时如何删除表格中的行? 使用Javascript - How to delete a row in the table when I click on a button? Using Javascript 如何使用 javaScript 在按钮单击时从表中删除特定行 - how to delete specific row from table on button click using javaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM