javascript - 将文本从表格中的特定数据单元格复制到剪贴板。
问题描述
我正在尝试在我的 php 文件中使用 JavaScript 函数从表中的特定单元格复制数据。每行都有一列用于项目、描述和项目链接。
我希望能够通过单击按钮将链接复制到剪贴板。但是,无论我单击哪一行复制链接,它都只复制最后一个。
我的 Javascript 函数只接受表格上显示的最后一个 ID 变量,而不是我要复制的变量的 ID...
while ($row = $result->fetch_assoc()) {
$id = $row['id'];
echo '<tr align="center">
<td>'.$row["pid"].':<br>'.$row["pname"].'</td>
<td>' .$row["URLdescription"].'</td>
<td>
<input type="text" value="'.$row['directionsURL'].'" id="'.$id.'">
<button onclick="myFunction()" name="copy"> Copy </button>
</td>
</tr>';
}; // END WHILE
^^ 这是我的while循环
<script>
function myFunction() {
var copyText = document.getElementById("<?php echo $id; ?>")
copyText.select();
document.execCommand("copy");
alert("Copied the text: " + copyText.value);
}
</script>
^^这是我的 JavaScript 函数。
解决方案
html
<input type="text" value="
<?php echo $row['account']; ?>" id="
<?php echo "myInput".$id ;?>">
<button class = "abc btn-sm btn-success" data-id1="
<?php echo "myInput".$id ;?>">
Copy
</button>
JS
$(document).on('click', '.abc', function(){
var id = $(this).data("id1");
var copyText = document.getElementById(id);
/* Select the text field */
copyText.select();
copyText.setSelectionRange(0, 99999); /*For mobile devices*/
/* Copy the text inside the text field */
document.execCommand("copy");
/* Alert the copied text */
alert("Copy: " + copyText.value);
});
推荐阅读
- arrays - Go 在数组子切片上的内置范围具有不一致的行为
- python - 修改过滤数据框的值
- ios - 如何使用目标 c 中的 UIImage 数组从 Images.xcassets 文件夹中加载和动画图像
- node.js - Firebase 云函数 - 意外令牌“。”
- java - 从 Jboss 中的 HttpServerletRequest 获取 LDAP 用户信息
- c# - 来自 PrintServer 默认构造函数的随机 Win32 错误(.Net 4.5 - System.Printing.PrintServer)
- python - 需要在 web alp 上执行 CLI 的最佳实践?
- python - 为什么这些假定相同的 Python 字典表现不同?
- python - 如何从列表中获取n个单词的连续单词组合
- flutter - 在 Flutter_map 中添加多个多边形