我正在嘗試foreach使用 . 將使用循環生成的值復制到剪貼板JS。無論單擊哪一行,都只會復制第一行。以下是生成要復制的值的代碼:foreach($results_array as $value) { /*Some PHP code*/ <input class="col-sm-10" title="Copy Link" type="text" id="copy_this" value='<?php echo $user_folder."/".$value; ?>' onclick="copyTo()"/>}我的JS函數:function copyTo() { /* Get the text field */ var copyText = document.getElementById("copy_this"); /* Copy the text inside the text field */ document.execCommand("copy");}
1 回答

qq_笑_17
TA貢獻1818條經驗 獲得超7個贊
ID 的全部目的是唯一地標識一個元素。另外,這不是execCommand()工作原理(您的代碼怎么可能知道您想要復制的文本?)。
擺脫 ID(你根本不需要),你可以這樣做:
function copyTo(input) {
input.select();
document.execCommand("copy");
}
<input type="text" value="First" onclick="copyTo(this)">
<input type="text" value="Second" onclick="copyTo(this)">
<input type="text" value="Third" onclick="copyTo(this)">
- 1 回答
- 0 關注
- 146 瀏覽
添加回答
舉報
0/150
提交
取消