如何在HTML表格中获取行索引 [英] How to get the row index in a HTML table

查看:691
本文介绍了如何在HTML表格中获取行索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更新数据库用户表. 我制作了一个HTML表,其中包含数据库中用户的数据. 该HTML表包含3列,每行中都有一个按钮.当我单击此按钮时,应使用输入中包含的信息更新用户数据库表,并在html表的单元格中选择字段. 我的html代码是:

I'm trying to update a database users table. I made an HTML table which contains the data of the users in my database. this HTML table contains 3 columns and in each row there is a button. when I click this button I should update the users database table with the information contained in the input and select field in the cells of the html table. my html code is:

<tr>
  <td><input type="text" name ="name"/></td>
  <td>
      <select name="select_job">
         <option value="1"> Engineer </option>
         <option value="2"> Doctor </option>
      </select>
  </td>
  <td><button>update </button></td>
</tr>

我试图获取单击了更新按钮的行数,然后获取每一列中的数据,但是我失败了. 我的js代码是:

I tried to get the number of the rows on which the update button was clicked and then to get the data in each column but I failed . my js code is:

$("button").click( function() {
 var $row = $(this).parent().parent(); //tr
  var $columns = $row.find('td');
  $.each($columns, function(i, item) {

   values = values + 'td' + (i ) +':<br/>';


});

 });

如何在单击按钮的行中获取数据(插入文本,所选项目)?

How can I get the data (inpt text, selected item) in the row on which I clicked the button?

推荐答案

您可以这样使用

$("button").click(function () {
    var inputValue = $(this).closest("tr").find("input[type=text]").val();
    var selectValuse = $(this).closest("tr").find("[name='select_job']").val();
    var index = $(this).closest("tr").index();
});

这篇关于如何在HTML表格中获取行索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆