Copy button functionality

拈花ヽ惹草 提交于 2020-01-25 10:16:10

问题


I have this Bootstrap code which I would like to use to generate address and implement copy button functionality:

<div class="modal fade" id="bitcoinModal" role="dialog">
                    <div class="modal-dialog modal-lg">
                      <div class="modal-content">
                        <div class="container">
                            <div class="offset-top-20 text-md-left">
                              <button type="button" class="close" data-dismiss="modal">&times;</button>
                              <h3>Copy address</h3>
                            </div>
                            <div class="section-60 offset-top-35">

                                <div class="offset-top-20 text-md-center">
                                  <form class="rd-mailform form-inline-custom text-left" data-form-output="form-output-global" data-form-type="subscribe" method="post" action="http://.........">
                                    <div class="form-group form-group-outside">
                                      <div class="input-group">
                                        <label class="form-label form-label-outside text-dark" for="forms-subscribe-email">Bitcoin Address</label>
                                        <input class="form-control" id="forms-subscribe-email" type="text" name="bitcoin_address" value="3J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy " data-constraints="@Required"/>
                                      </div>
                                      <div class="input-group-btn">
                                        <button class="btn btn-width-165 btn-primary" type="submit">Copy</button>
                                      </div>
                                    </div>
                                  </form>
                                </div>

                            </div>
                          </div>
                      </div>
                    </div>
                  </div>
              </div>

How I can copy the content from the input item into the clipboard? How I can change the text to "Copied"


回答1:


This should work:

function copyToClipboard(e, btn) {
  e.preventDefault();     // prevent submit
  var str = document.getElementById("forms-subscribe-email");
  str.select();
  document.execCommand('copy');
  btn.innerHTML = "Copied!";
  return false;           // prevent submit
}
<div class="modal fade" id="bitcoinModal" role="dialog">
  <div class="modal-dialog modal-lg">
    <div class="modal-content">
      <div class="container">
        <div class="offset-top-20 text-md-left">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h3>Copy address</h3>
        </div>
        <div class="section-60 offset-top-35">

          <div class="offset-top-20 text-md-center">
            <form class="rd-mailform form-inline-custom text-left" data-form-output="form-output-global" data-form-type="subscribe" method="post" action="http://.........">
              <div class="form-group form-group-outside">
                <div class="input-group">
                  <label class="form-label form-label-outside text-dark" for="forms-subscribe-email">Bitcoin Address</label>
                  <input class="form-control" id="forms-subscribe-email" type="text" name="bitcoin_address" value="3J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy " data-constraints="@Required" />
                </div>
                <div class="input-group-btn">
                  <button class="btn btn-width-165 btn-primary" onclick="return copyToClipboard(event, this);">Copy</button>
                </div>
              </div>
            </form>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>



回答2:


function copyKey(id) {
            var copyText = document.getElementById(id);
            copyText.select();
            document.execCommand("copy");
        }

        function copy_IP() {
            var copy_Text = document.getElementById('our_ip');

            //create temporary input to copy text as our input is hidden so we need to create another input 
            var tempInput = document.createElement("input");
            tempInput.style = "position: absolute; left: -1000px; top: -1000px";
            tempInput.value = copy_Text.value;
            document.body.appendChild(tempInput);
            tempInput.select();
            document.execCommand("copy");
            document.body.removeChild(tempInput);

            //show success message
            toastr.info(copyText.value, 'IP Copied to Clipboard');
        }

You can use any of above function




回答3:


Use execCommand for this.

function myFunction() {
  var copyTextfield = document.getElementById("myInput");
  copyTextfield.select();
  document.execCommand("copy");
  alert("Copied the text: " + copyTextfield.value);
  copyTextfield.value = "Copied";
}
<input type="text" value="Hello World" id="myInput">
<button onclick="myFunction()">Copy text</button>


来源:https://stackoverflow.com/questions/59261438/copy-button-functionality

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!