gpt4 book ai didi

javascript - 如何获取 HTML 表格中的行索引

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:15:08 25 4
gpt4 key购买 nike

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

<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 代码是:

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

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


});

});

如何获取点击按钮所在行的数据(inpt text,selected item)?

最佳答案

你可以这样使用

$("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();
});

关于javascript - 如何获取 HTML 表格中的行索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23755390/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com