gpt4 book ai didi

javascript - 如何将 2 个参数传递给 jquery Find() 方法

转载 作者:行者123 更新时间:2023-11-28 18:46:29 24 4
gpt4 key购买 nike

我创建了一个显示数据的表格,当用户单击更新按钮时,它将表格单元格转换为用户可以输入数据的输入字段。

当我单击编辑按钮时,它成功地将表格的 1 个单元格更改为输入字段,但我无法理解如何更改表格的其他单元格,我需要通过单击更改表格的所有单元格。

$(document).on("click", ".updateUser", function() {
$(this).closest('tr').find('td:nth-child(2)').each(function() {
var html = $(this).html();
var input = $('<input type="text" />');
input.val(html);
$(this).html(input);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table class="table table-hover">
<thead>
<tr>
<th>#</th>
<th>Username</th>
<th>Password</th>
<th>Role</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<tr>
<td>ID</td>
<td>Username </td>
<td>Password </td>
<td>Role </td>

<td>

<button type="button" class="updateUser btn btn-primary btn-xs" data-userId="<?php echo $id; ?>">
EDIT BUTTON
</button>
</td>
</tr>
</tbody>
</table>

我已经在 JSFiddle 上添加了示例,请指导我如何实现这一目标。

https://jsfiddle.net/tahakirmani/g7vrskpz/3/

最佳答案

从选择器中删除nth-child(2)nth-child(2) 仅导致第 n 个(在本例中为第二个)元素被选择并循环/由输入框替换。

https://jsfiddle.net/g7vrskpz/4/

 $(document).on("click", ".updateUser", function() {
$(this).closest('tr').find('td').each(function() {
var html = $(this).html();
var input = $('<input type="text" />');
input.val(html);
$(this).html(input);
});

});

编辑:仅限前 3 个

https://jsfiddle.net/g7vrskpz/5/

 $(document).on("click", ".updateUser", function() {
$(this).closest('tr').find('td:nth-child(1),td:nth-child(2),td:nth-child(3)').each(function() {
var html = $(this).html();
var input = $('<input type="text" />');
input.val(html);
$(this).html(input);
});

});

关于javascript - 如何将 2 个参数传递给 jquery Find() 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35232592/

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