Javascript Onclick输入值复制

时间:2017-12-04 12:09:36

标签: javascript jquery

你好伙计我想复制输入值是按钮点击但没有任何作用。请帮助!!

<script>
  function copyToClipboard(element) {
    var $temp = $("<input>");
    $("body").append($temp);
    $temp.val($(element).text()).select();
    document.execCommand("copy");
    $temp.remove();
  }
</script>   

<input type="text" class="form-control" name="myvalue"  id="myvalue" value="YEAH" readonly  />

<button class="btn btn-primary btn-block" onclick="copyToClipboard('#myvalue')">Copy myvalue</button>

你好伙计我想复制输入值是按钮点击但没有任何作用。请帮助!!

3 个答案:

答案 0 :(得分:1)

function copyToClipboard() {
    var textBox = document.getElementById("myvalue");
    textBox.select();
    document.execCommand("copy");
}
<input type="text" class="form-control" name="myvalue"  id="myvalue" value="YEAH" readonly  />

     <button class="btn btn-primary btn-block" onclick="copyToClipboard()">Copy myvalue</button>

答案 1 :(得分:0)

代码,如果要将输入文本复制到ClipBoard。

//HTML
<input type="text" class="form-control" name="myvalue"  id="myvalue" value="YEAH" readonly  />
<button class="btn btn-primary btn-block" onclick="copyToClipboard();">Copy myvalue</button>


//SCRIPT For ClipBoard Copy
 <Script>
    function copyToClipboard() {
        var textBox = document.getElementById("myvalue");
        textBox.select();
        document.execCommand("copy");
    }
</Script>



//For local storage variable copy
<Script>
    function copyToStorage() {
        var textBox = document.getElementById("myvalue");
        if(typeof(Storage) !== "undefined") {

            var textBox_value=textBox.value;
            localStorage.setItem("textBox_value", textBox_value);
         } else {
               alert("Sorry your browser does not support this script");
           }
    }
</Script>

   // Also there one to use an element like hidden to store the value for same page life.

答案 2 :(得分:-1)

你可以使用venilla js:

&#13;
&#13;
function copyToClipBoard(){
	var text = document.getElementById('myvalue'); 
  	text.select();
        document.execCommand("copy");
}
&#13;
 <input type="text" class="form-control" name="myvalue"  id="myvalue" value="HEllo" readonly  />

<button class="btn btn-primary btn-block" onClick="copyToClipBoard()" id="btn" >Copy myvalue</button>
&#13;
&#13;
&#13;