如何在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();
});