2017-08-02 44 views
1

有一個表由許多帶有列的表格行組成。從一個元素中獲取一個值並添加到每個href的末尾

我想從每個td的類名:routecode,我必須將該值添加到每個tr的最接近href的末尾。我會將此添加到路由代碼鏈接的值。

我寫了下面的代碼,但它不起作用,值不會添加到鏈接的結尾。

這裏是我的代碼:

$(".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>

+1

路線代碼被添加到鏈接的末尾。有什麼問題? –

+1

其增加。只要檢查元素 –

+1

他們是誰,請再次檢查您的代碼 –

回答

0

你可以試試這個代碼:

你缺少分號線;結束..

演示在這裏:https://output.jsbin.com/nolomec

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> 
+0

Routecode沒有被追加到問題中? –

+0

是'routecode'沒有附加在url中.. –

相關問題