简体   繁体   English

从元素获取值并添加到每个href的末尾

[英]get a value from an element and add to the end of each href

There is a table that consist of many table rows with columns. 有一个表,其中包含许多具有列的表行。

and I'd take a value from each td with classname: routecode and I have to add that value to the end of closest href of each tr. 我将从类名的每个td中获取一个值: routecode ,并将该值添加到每个tr的最接近href的末尾。 I would add this to the value of routecode link. 我将其添加到routecode链接的值。

I wrote the below code but it does not work and the value is not added to the end of link. 我写了下面的代码,但它不起作用,并且该值未添加到链接的末尾。

Here is my code: 这是我的代码:

 $(".list").each(function(index, element) { var routecode = $(this).closest("tr").find(".routecode").text() var linkk = $(this).attr("href") $(this).attr("href", linkk + routecode); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> <html> <head> </head> <body> <table border="1"> <tr class=" odd"> <td class="routecode">TK26</td> <td> <a class="list" href="/reservation/route/info.bc?dmnid=2893&routecode=">Edit </a> </td> </tr> <tr class=" odd"> <td class="routecode">tkeee</td> <td> <a class="list" href="/reservation/route/info.bc?dmnid=2893&routecode=">Edit </a> </td> </tr> </table> </body> </html> 

You can try this code: 您可以尝试以下代码:

You are missing semicolon ; 你缺少分号; end of line.. 行结束..

Demo here: https://output.jsbin.com/nolomec 演示在这里: https : //output.jsbin.com/nolomec

https://jsbin.com/nolomec/2/edit?html https://jsbin.com/nolomec/2/edit?html

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
   $(".list").each(function(index, element) {
    var routecode = $(this).closest("tr").find(".routecode").text();
    var linkk = $(this).attr("href");
    $(this).attr("href", linkk + routecode);
  });
});
</script>
</head>
<body>

<table border="1">
<tr class="odd">
<td class="routecode">TK26</td>
<td>                                       
<a class="list" href="/reservation/route/info.bc?dmnid=2893&routecode=">Edit </a>
</td>                                   
</tr>

<tr class=" odd">
<td class="routecode">tkeee</td>
 <td>                                       
<a  class="list" href="/reservation/route/info.bc?dmnid=2893&routecode=">Edit </a>
</td>                                   
</tr>                             
</table>

</body>
</html>

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

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