简体   繁体   English

获取表格的行索引javascript

[英]Get row index of a table javascript

I know this question has been asked many times, I went through most of the suggestions and none of the solutions seem to be working for me. 我知道这个问题已经问了很多遍了,我经历了大多数建议,但似乎没有一个解决方案对我有用。

I have a table in a modal in which I am allowing the user to make some changes. 我有一个模态表,允许用户进行一些更改。 I then use these values to update the data in the database. 然后,我使用这些值来更新数据库中的数据。

My table rows are created by echoing the HTML code 我的表行是通过回显HTML代码创建的

echo "<tr> ";                   
echo "<td > $row_counter  </td>";
echo "<td style='width:200px' class='left_align' > $lstr_product_name </td>";
echo "<td  > $lstr_department_name </td>";
echo "<td  > <input type='text' name='unit_cost' class='form-control unit_cost' style='width:50px;' value='$lint_unit_cost' /> <input name='product_id' data-id='$lint_product_id' class='form-control product_id' value='$lint_product_id' type='hidden'/> </td>";
echo "<td  > $lint_quantity_counted </td>";
echo "<td  > $lint_total_cost </td>";

To update the proper values in the database I need to get the product_id name which I know that I can get by 要更新数据库中的适当值,我需要获取product_id名称,该名称我知道可以通过

var x = document.getElementsByName("product_id")[0].value;

However I need to get the correct row index to be able to POST the correct product_id. 但是,我需要获取正确的行索引才能发布正确的product_id。

I have tried the following: 我尝试了以下方法:

alert($(this).index()); 

But this always returns 0. The closest I got to a solution was by using 但这总是返回0。我最接近解决方案的是使用

var rowID = $(this).closest('tr').index();
alert(rowID);

The problem with this is that my editable cell is "unit_cost" which is the 4th cell in the tr. 问题是我的可编辑单元格是“ unit_cost”,它是tr中的第四个单元格。 This means that the closest tr is not the one that the row that the cell is in but the one below. 这意味着最接近的tr不是单元格所在的行,而是下面的行。

I have then tried 然后我尝试了

alert( "Row id: " + $("#tbl_store_product_sizes tr").this.rowIndex );

But also no luck. 但也没有运气。 I have ran out of ideas, what is the proper way of doing this? 我的想法已经用光了,这样做的正确方法是什么?

EDIT All my javascript code is enclosed in the $(document).ready(function() I am watching for the change of the unit_cost field using the following code 编辑我所有的JavaScript代码都包含在$(document).ready(function()中,我正在使用以下代码监视unit_cost字段的更改

$(document.body).off( "change", ".unit_cost");
$(document.body).on('change', '.unit_cost', function(event) 
{

    var rowID = $(this).closest('tr').index();
    alert(rowID);
    var product_id = document.getElementsByName("product_id")[rowID].value;
    alert('The product id is: ' + x);
    var unit_cost = document.getElementsByName("unit_cost")[rowID].value;


});

Explanation:- 说明:-

Since both inputs are in the same <td> , so on change of first-one you have to use .next() to get next input data. 由于两个输入都在同一个<td> ,因此在更改第一个输入时,必须使用.next()获取下一个输入数据。

You need to do it like below:- 您需要像下面这样:

$(document).on('change', '.unit_cost', function(event) {

    var rowID = $(this).closest('tr').index();
    alert(rowID);
    var product_id = $(this).next('.product_id').val();
    alert('The product id is: ' + product_id);
    var unit_cost = $(this).val();
});

Working snippet:- 工作片段:-

 $(document).on('change', '.unit_cost', function(event) { var rowID = $(this).closest('tr').index(); alert('The corresponding row index is: '+ rowID); var product_id = $(this).next('.product_id').val(); alert('The product id is: ' + product_id); var unit_cost = $(this).val(); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr> <td>1</td> <td style='width:200px' class='left_align'>2 </td> <td>3 </td> <td> <input type='text' name='unit_cost' class='form-control unit_cost' style='width:50px;' value='4' /> <input name='product_id' data-id='$lint_product_id' class='form-control product_id' value='5' type="hidden"/> </td> <td>6</td> <td>7</td> </tr> <tr> <td>8</td> <td style='width:200px' class='left_align'>9 </td> <td>10 </td> <td> <input type='text' name='unit_cost' class='form-control unit_cost' style='width:50px;' value='11' /><input name='product_id' data-id='$lint_product_id' class='form-control product_id' value='12' type="hidden"/> </td> <td>13</td> <td>14</td> </tr> </table> 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM