javascript - 复制命令不适用于所有动态生成的表项 - jsp spring boot
问题描述
我正在创建一个包含 4 列(id、DestinationURL、ShortLink、Action(copy))的表。所有数据都来自数据库,我正在我的 JSP 页面中使用 for-each 循环创建内容。输出页面看起来像这样
在操作列之前有一个隐藏字段,其中包含 baseURL+ ShortURL 即。这是我创建此表的代码http://localhost:8080/qcX4h6
<table class="table table-responsive table-striped table-hover table-bordered vertical-alien-middle">
<thead class="thead-dark">
<tr>
<th>Id</th>
<th>Destination Url</th>
<th>Short URL</th>
<!--<th width="20%" colspan="3">Actions</th>-->
<th>Actions</th>
</tr>
</thead>
<tbody>
<c:forEach var="links" items="${data}">
<tr>
<td>${links.getId()}</td>
<td>${links.getDestinationUrlLink()}</td>
<td>${links.getShortUrlLink()}</td>
<td id="copyTd" hidden="">
<input type="text" id="${links.getId()}" value="${baseUrl}/${links.getShortUrlLink()}">
</td>
<td><button type="submit" class="btn btn-success btn-block" onclick="copyShortLink(${links.getId()})">
Copy
</button>
</td>
<!--<td><button type="submit" class="btn btn-primary btn-block">Edit</button></td>-->
<!--<td><button type="submit" class="btn btn-danger btn-block">Delete</button></td>-->
</tr>
</c:forEach>
</tbody>
</table>
复制命令适用于第一个按钮,但不适用于所有后续按钮。这是我的javascript函数
function copyShortLink(id) {
var input = document.getElementById(id);
var td = document.getElementById("copyTd");
/* Select the text field */
td.hidden = false;
input.select();
input.setSelectionRange(0, 99999); /*For mobile devices*/
/* Copy the text inside the text field */
document.execCommand("copy");
td.hidden = true;
}
所以我的问题是如何使其他按钮具有相同的功能。我在函数中传递了隐藏输入字段的 id,所以为什么它只适用于第一个按钮而不适用于其他按钮。
任何帮助将不胜感激。提前致谢
解决方案
在您的代码 ie 中:id="copyTd"
这将被复制到第一行,因为相同的 id 不能用于所有行,而是您可以在<td id="">
ie 中附加一些值:copyTd_${links.getId()}
在这里,添加链接 id 以唯一标识每个td
。只需添加document.getElementById("copyTd_" + id);
您js
的代码即可。您需要在代码中进行的更改如下:
jsp代码:
<tr>
<td>${links.getId()}</td>
<td>${links.getDestinationUrlLink()}</td>
<td>${links.getShortUrlLink()}</td>
<!--added links_id to your td-->
<td id="copyTd_${links.getId()}" hidden="">
...
</td>
</tr>
Javascript代码:
function copyShortLink(id) {
var input = document.getElementById(id);
/*add same id to select particular td*/
var td = document.getElementById("copyTd_" + id);
...
}
使用虚拟数据:
function copyShortLink(id) {
var input = document.getElementById(id);
var td = document.getElementById("copyTd_" + id);
/* Select the text field */
td.hidden = false;
input.select();
input.setSelectionRange(0, 99999); /*For mobile devices*/
/* Copy the text inside the text field */
document.execCommand("copy");
console.log("Text copied ..." + input.value);
td.hidden = true;
}
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<table class="table table-responsive table-striped table-hover table-bordered vertical-alien-middle">
<thead class="thead-dark">
<tr>
<th>Id</th>
<th>Destination Url</th>
<th>Short URL</th>
<!--<th width="20%" colspan="3">Actions</th>-->
<th>Actions</th>
</tr>
</thead>
<tbody>
<c:forEach var="links" items="${data}">
<tr>
<td>${links.getId()}</td>
<td>${links.getDestinationUrlLink()}</td>
<td>abc.in</td>
<td id="copyTd_abc" hidden="">
<input type="text" id="abc" value="abc.in">
</td>
<td><button type="submit" class="btn btn-success btn-block" onclick="copyShortLink('abc')">
Copy
</button>
</td>
</tr>
<tr>
<td>${links.getId()}</td>
<td>${links.getDestinationUrlLink()}</td>
<td>abcd.com</td>
<td id="copyTd_abcd" hidden="">
<input type="text" id="abcd" value="abcd.com">
</td>
<td><button type="submit" class="btn btn-success btn-block" onclick="copyShortLink('abcd')">
Copy
</button>
</td>
</tr>
</c:forEach>
</tbody>
</table>
推荐阅读
- flutter - MatrixGestureDetector 设置为身份
- php - 使用 eloquent 和 laravel 从多个 sql 表中获取数据
- javascript - 子组件不会重新渲染
- python-3.x - Python lxml Xpath 导入问题 - 我需要删除附加到 imoprted 值的前导和尾随 ['']
- flutter - 如何将颜色转换为 Dart & Flutter 中的 RGB 值列表?
- flutter - Flutter in_app_purchase purchaseUpdatedStream.listen 在 iOS 上不起作用在 android 上效果很好
- facebook-graph-api - localtunnel 未在 Windows 10 上生成 url 字符串
- python - 无法使用 bs4 中的 select 和 find 方法提取值
- vue.js - 如果在 v-for 中找不到数据,则隐藏文本
- ios - 如何限制对我的 REST API 的访问?