简体   繁体   English

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

[英]How to pass 2 arguments to jquery Find() method

I have created a table that displays data, when user click on Update button it turns table cells into input field where user can enter the data.. 我创建了一个显示数据的表,当用户单击“ Update按钮时,它将表单元格转换为用户可以在其中输入数据的输入字段。

When i click on edit button it successfully changes 1 cell of table into input field, but i am unable to understand how to change other cells of the table, i need to change all cells of the table in single click. 当我单击“编辑”按钮时,它成功地将表的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> 

I have added the example on JSFiddle kindly guide me how to achieve this. 我在JSFiddle上添加了示例,请指导我如何实现此目标。

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

Remove nth-child(2) from the selector. 从选择器中删除nth-child(2) nth-child(2) causes only the nth (in this case, second) element to be selected and looped through / replaced by an input box. nth-child(2)仅导致第n个元素(在本例中为第二个)被选中,并通过输入框循环/替换。

https://jsfiddle.net/g7vrskpz/4/ 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);
   });

 });

Edit: First 3 only 编辑:仅前3个

https://jsfiddle.net/g7vrskpz/5/ 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);
   });

 });

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

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