简体   繁体   English

如何在jQuery中编辑表的所有TR的值

[英]how to edit values of all TR of a table in jQuery

I have the following code and would like to change the value of the input and when I edit the TR values would change automatically. 我有以下代码,想更改输入的值,当我编辑TR值时会自动更改。

 $('.change').change(function() { var element = $(this).parent(); console.log('get result: ' + $(this).parent().find('.resultt').val()); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <tbody> <form action='' method='POST'> <tr> <td> ASDADAS </td> <td> 2 </td> <td> 2017-08-21 </td> <td class='money'> 130.00 </td> <td> <input type='text' class='change' style='width: 30%;' value='0.033%' /> </td> <td class='resultt'> 130.04 </td> <td class='parcial'> 0.04 </td> </tr> </form> <form action='' method='POST'> <tr> <td> XXFDFD </td> <td> 1 </td> <td> 2017-08-20 </td> <td class='money'> 121.34 </td> <td> <input type='text' class='change' style='width: 30%;' value='0.033%' /> </td> <td class='resultt'> 120.36 </td> <td class='parcial'> 0.34 </td> </tr> </form> </tbody> 

I tried using the script to see if I could get the value of the CLASS so I could edit it. 我尝试使用脚本来查看是否可以获取CLASS的值,以便对其进行编辑。 But I have not been successful. 但是我没有成功。

The parent of the input is the cell, not the row. 输入的父级是单元格,而不是行。 Try calling parent twice, or even better try using .closest("tr") . 尝试两次调用parent,或者甚至最好使用.closest("tr")

You need to use $(this).closest('tr').find('.resultt').text() 您需要使用$(this).closest('tr').find('.resultt').text()

td will give you text() and not val() td会给你text()而不是val()

 $('.change').change(function() { var element = $(this).parent(); console.log('get result: ' + $(this).closest('tr').find('.resultt').text()); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tbody> <form action='' method='POST'> <tr> <td> ASDADAS </td> <td> 2 </td> <td> 2017-08-21 </td> <td class='money'> 130.00 </td> <td> <input type='text' class='change' style='width: 30%;' value='0.033%' /> </td> <td class='resultt'> 130.04 </td> <td class='parcial'> 0.04 </td> </tr> </form> <form action='' method='POST'> <tr> <td> XXFDFD </td> <td> 1 </td> <td> 2017-08-20 </td> <td class='money'> 121.34 </td> <td> <input type='text' class='change' style='width: 30%;' value='0.033%' /> </td> <td class='resultt'> 120.36 </td> <td class='parcial'> 0.34 </td> </tr> </form> </tbody> <table> 

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

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