Javascript 使用jQuery设置表单元格中的链接
我有一张有三个td的桌子。在第三个td中,我想动态地设置一个包含来自其他两个td小区的数据的链路。Javascript 使用jQuery设置表单元格中的链接,javascript,jquery,html,Javascript,Jquery,Html,我有一张有三个td的桌子。在第三个td中,我想动态地设置一个包含来自其他两个td小区的数据的链路。 我知道如何更改链接,但每行都从第一行获取链接。也许是一个非常简单的解决方案,但我有点无助 到目前为止我的代码: HTML: <table id="table"> <tbody> <tr> <td class="one">11</td> <td class="two">12</t
我知道如何更改链接,但每行都从第一行获取链接。也许是一个非常简单的解决方案,但我有点无助 到目前为止我的代码:
HTML:
<table id="table">
<tbody>
<tr>
<td class="one">11</td>
<td class="two">12</td>
<td class="three"><a href='#'>13</a></td>
</tr>
<tr>
<td class="one">21</td>
<td class="two">22</td>
<td class="three"><a href="#">23</a></td>
</tr>
</tbody>
</table>
$(document).ready(function () {
var row = $('#table .three a').closest('tr');
var td = row.find('td');
var id1 = td.eq(0).text();
var id2 = td.eq(1).text();
$('#table .three a').attr("href", "test.html?" + id1 + "-" + id2);
});
您可以使用以下功能:
$('#table .three a').each(function () {
var id1 = $(this).closest('tr').find('td:eq(0)').text();
var id2 = $(this).closest('tr').find('td:eq(1)').text();
$(this).prop("href", "test.html?" + id1 + "-" + id2);
});
将您的javascript更改为
$(document).ready(function () {
$('#table .three a').each(function(){
var row = $(this).closest('tr');
var td = row.find('td');
var id1 = td.eq(0).text();
var id2 = td.eq(1).text();
$(this).attr("href", "test.html?" + id1 + "-" + id2);
})
});
你需要使用
“#table.three a”返回两个匹配项,从那时起,您需要一个each()操作/循环来分别处理它们…该死,找到答案;-)
$('#table .three a').each(function (index) {
var row = $(this).closest('tr');
var td = row.find('td');
var id1 = td.eq(0).text();
var id2 = td.eq(1).text();
$(this).attr("href", "test.html?" + id1 + "-" + id2);
});