【发布时间】:2016-10-13 13:40:27
【问题描述】:
假设我有一个如下所示的表格:
<table>
<tbody>
<tr class="card-id" data-id="1">
<td>
<a href="http://www.test.nl/">Card 1</a>
<input class="option" type="checkbox" />
</td>
</tr>
<tr class="card-id" data-id="2">
<td>
<a href="http://www.test.nl/">Card 2</a>
<input class="option" type="checkbox" />
</td>
</tr>
<tr class="card-id" data-id="3">
<td>
<a href="http://www.test.nl/">Card 3</a>
<input class="option" type="checkbox" />
</td>
</tr>
</tbody>
</table>
是否可以获取已检查的表行的数据ID,然后将它们附加到url?
我尝试在 js 中使用此代码
$(".option").click(function () {
var id = $(this).closest('.card-id').data('id');
$(".options-count a").each(function () {
var $this = $(this);
var _href = $this.attr("href");
$this.attr("href", _href + 'card-id=' + id);
});
});
得到了这个
<a href="http://www.test.nl/card-id=1card-id=2"></a>
但我希望得到类似的东西
<a href="http://www.test.nl/result?card_id=1&card_id=2">Card 1</a>
【问题讨论】:
-
如何从 data-id 中选择?
-
那么你会创建另一个
<a>标签吗? -
代码已编辑,请检查是否有什么可以改进的地方
-
@RaxWeber 不,只需将其附加到现有的 hrefs 中
标签: javascript jquery append href custom-data-attribute