简体   繁体   English

使用jQuery将表单元格文本更改为输入值

[英]change table cell text to input value with jquery

I have a table in CRUD loaded from database, i want table rows to be editable by converting table cell to input field preserving cell text as input value. 我有一个从数据库装入的CRUD中的表,我希望通过将表单元格转换为保留单元格文本作为输入值的输入字段来可编辑表行。 I am using jQuery for that. 我为此使用jQuery。 So far i can add input field inside tag. 到目前为止,我可以在标签内添加输入字段。 But data from all the cells becomes value of each input instead of becoming value of their respective input fields. 但是,来自所有单元格的数据将成为每个输入的值,而不是成为其各自输入字段的值。 Here is my code 这是我的代码

$("#parEdit").click(function(){
$(this).text("Update");
$(this).closest('tr').children().wrapInner('<input type="text" value = ' + $('td').text() + '></input>');
    })

table ID| 表ID | Name | 姓名| Position | 职位| Marks| 商标| Edit| 编辑|

1 | 1 | Ahmad | 艾哈迈德| 6th | 6号| 525 | 525 | Edit Button| 编辑按钮|

After pressing Edit Button The output is like this 按下编辑按钮后,输出是这样的

ID| ID | Name | 姓名| Position | 职位| Marks| 商标| Edit| 编辑|

1Ahmad6th525 | 1Ahmad6th525 | 1Ahmad6th525 | 1Ahmad6th525 | 1Ahmad6th525 | 1Ahmad6th525 | 1Ahmad6th525 | 1Ahmad6th525 | Update Button | 更新按钮|

You need apply the wrap to each td, your original code inserted every td text. 您需要对每个td应用自动换行,将原始代码插入每个t​​d文本。

$("#parEdit").click(function(){
   $(this).text("Update");
   $(this).closest('tr').children().each(function(){
      $(this).wrapInner('<input type="text" value = ' + $(this).text() + '></input>');
   });
});

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

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