简体   繁体   English

将文本从隐藏的输入复制到剪贴板

[英]Copy text to clipboard from input hidden

I tried to copy by pressing a button, copy the value of input hidden.我试图通过按下按钮来复制,复制隐藏输入的值。 In JavaScript the value is obtained in var without problems, but it does not copy the value.在 JavaScript 中,在 var 中获得的值没有问题,但它不会复制该值。 What am I doing wrong?我究竟做错了什么?

 <div class="dropdown-divider"></div>
  <a class="dropdown-item" onclick="mycopyphone()">Copiar Telefono</a>    
   <input type="hidden" id="Key" value="'. $row["telefono"] .'" />
     <script>
          function mycopyphone() {
           var hidden = document.getElementById("Key").value;
            copyText = hidden;
             copyText.select();
              copyText.setSelectionRange(0, 99999)
               document.execCommand("copy");
               alert("Copied the text: " + copyText.value);
                }
       </script>

Two problems.两个问题。 Hidden inputs don't support text selection, and it's the input element that has the select() function, not its value.隐藏输入不支持文本选择,它是具有 select() function 的输入元素,而不是它的值。 You could do this instead:你可以这样做:

<div class="dropdown-divider"></div>
<a class="dropdown-item" onclick="mycopyphone()">Copiar Telefono</a>
<input type="text" style="display:none;" id="Key" value="'. $row["telefono"] .'" />
<script>
  function mycopyphone() {
    var hidden = document.getElementById("Key");
    hidden.style.display = 'block';
    hidden.select();
    hidden.setSelectionRange(0, 99999)
    document.execCommand("copy");
    alert("Copied the text: " + hidden.value);
    hidden.style.display = 'none';
  }
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM