我试图从id kode中获得与我的脚本不同的值。我的脚本只获得第一个值。
<table id="myTable" style="margin-left: 50px;">
<tr class="header">
<th style="width:30%;"></th>
<th style="width:30%;"></th>
<th style="width:30%;"></th>
</tr>
<?php
foreach ($barang as $br)
{ ?>
<tr>
<td><?php echo $br->nama_barang ?></td>
<td><input type="text" id="kode<?php echo $br->kode_barang ?>" value="<?php echo $br->kode_barang ?>" readonly="true"></td>
<td><button style="color: blue;" onclick="getkode<?php echo $br->kode_barang ?>()">get</button></td>
</tr>
<?php }
?>
</table>
脚本
function getkode() {
var copyText = document.getElementById("kode");
copyText.select();
document.execCommand("copy");
alert("Copied the text: " + copyText.value);
}
答案 0 :(得分:1)
好,这就是我要做的
更改这些:
<td><button style="color: blue;" onclick="getkode<?php echo $br->kode_barang ?>()">get</button></td>
对此:
<td><button style="color: blue;" onclick="getkode('<?php echo $br->kode_barang ?>')">get</button></td>
因此,您将$br->kode_barang
作为点击函数的参数传递。然后:
function getkode(kode_barang) {
var copyText = document.getElementById("kode"+kode_barang);
copyText.select();
document.execCommand("copy");
alert("Copied the text: " + copyText.value);
}
PS。我前一段时间写了这个jQuery剪贴板插件,欢迎您使用它
https://github.com/ArtisticPhoenix/jQuery-Plugins/blob/master/jqClipboard.js
如果我没记错我的代码,您会做类似
的操作$('body').jqClipboard('copy', html);
它使用动态创建的文本区域来复制内容。因此它可以将任何HTML复制到剪贴板,甚至只是字符串。