• <small id='uPL6y'></small><noframes id='uPL6y'>

    • <bdo id='uPL6y'></bdo><ul id='uPL6y'></ul>
  • <tfoot id='uPL6y'></tfoot>
    <i id='uPL6y'><tr id='uPL6y'><dt id='uPL6y'><q id='uPL6y'><span id='uPL6y'><b id='uPL6y'><form id='uPL6y'><ins id='uPL6y'></ins><ul id='uPL6y'></ul><sub id='uPL6y'></sub></form><legend id='uPL6y'></legend><bdo id='uPL6y'><pre id='uPL6y'><center id='uPL6y'></center></pre></bdo></b><th id='uPL6y'></th></span></q></dt></tr></i><div id='uPL6y'><tfoot id='uPL6y'></tfoot><dl id='uPL6y'><fieldset id='uPL6y'></fieldset></dl></div>

    1. <legend id='uPL6y'><style id='uPL6y'><dir id='uPL6y'><q id='uPL6y'></q></dir></style></legend>
      1. 将 JavaScript 变量的输出复制到剪贴板

        时间:2023-10-02
          <tbody id='iXBtl'></tbody>

        <small id='iXBtl'></small><noframes id='iXBtl'>

            1. <i id='iXBtl'><tr id='iXBtl'><dt id='iXBtl'><q id='iXBtl'><span id='iXBtl'><b id='iXBtl'><form id='iXBtl'><ins id='iXBtl'></ins><ul id='iXBtl'></ul><sub id='iXBtl'></sub></form><legend id='iXBtl'></legend><bdo id='iXBtl'><pre id='iXBtl'><center id='iXBtl'></center></pre></bdo></b><th id='iXBtl'></th></span></q></dt></tr></i><div id='iXBtl'><tfoot id='iXBtl'></tfoot><dl id='iXBtl'><fieldset id='iXBtl'></fieldset></dl></div>
                <bdo id='iXBtl'></bdo><ul id='iXBtl'></ul>
              • <legend id='iXBtl'><style id='iXBtl'><dir id='iXBtl'><q id='iXBtl'></q></dir></style></legend>
              • <tfoot id='iXBtl'></tfoot>

                  本文介绍了将 JavaScript 变量的输出复制到剪贴板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我对 JavaScript 一无所知,但我设法使用来自各种 Stack Overflow 答案的点点滴滴将这段代码组合在一起.它工作正常,并通过警告框输出文档中所有选定复选框的数组.

                  I have no knowledge of JavaScript, but I managed to put this code together using bits and bolts from various Stack Overflow answers. It works OK, and it outputs an array of all selected checkboxes in a document via an alert box.

                  function getSelectedCheckboxes(chkboxName) {
                    var checkbx = [];
                    var chkboxes = document.getElementsByName(chkboxName);
                    var nr_chkboxes = chkboxes.length;
                    for(var i=0; i<nr_chkboxes; i++) {
                      if(chkboxes[i].type == 'checkbox' && chkboxes[i].checked == true) checkbx.push(chkboxes[i].value);
                    }
                    return checkbx;
                  }
                  

                  我用它来称呼它:

                  <button id="btn_test" type="button" >Check</button>
                  <script>
                      document.getElementById('btn_test').onclick = function() {
                          var checkedBoxes = getSelectedCheckboxes("my_id");
                          alert(checkedBoxes);
                      }
                  </script>
                  

                  现在我想修改它,所以当我单击 btn_test 按钮时,输出数组 checkbx 被复制到剪贴板.我尝试添加:

                  Now I would like to modify it so when I click the btn_test button the output array checkbx is copied to the clipboard. I tried adding:

                  checkbx = document.execCommand("copy");
                  

                  checkbx.execCommand("copy");
                  

                  在函数的末尾,然后像这样调用它:

                  at the end of the function and then calling it like:

                  <button id="btn_test" type="button" onclick="getSelectedCheckboxes('my_id')">Check</button>
                  

                  但它不起作用.没有数据被复制到剪贴板.

                  But it does not work. No data is copied to clipboard.

                  推荐答案

                  好的,我找到了一些时间,并按照 Teemu 我能够得到我想要的.

                  OK, I found some time and followed the suggestion by Teemu and I was able to get exactly what I wanted.

                  所以这里是任何可能感兴趣的人的最终代码.为澄清起见,此代码获取某个 ID 的所有选中复选框,将它们输出到一个数组中,此处命名为 checkbx,然后将它们的唯一名称复制到剪贴板.

                  So here is the final code for anyone that might be interested. For clarification, this code gets all checked checkboxes of a certain ID, outputs them in an array, named here checkbx, and then copies their unique name to the clipboard.

                  JavaScript 函数:

                  JavaScript function:

                  function getSelectedCheckboxes(chkboxName) {
                    var checkbx = [];
                    var chkboxes = document.getElementsByName(chkboxName);
                    var nr_chkboxes = chkboxes.length;
                    for(var i=0; i<nr_chkboxes; i++) {
                      if(chkboxes[i].type == 'checkbox' && chkboxes[i].checked == true) checkbx.push(chkboxes[i].value);
                    }
                    checkbx.toString();
                  
                    // Create a dummy input to copy the string array inside it
                    var dummy = document.createElement("input");
                  
                    // Add it to the document
                    document.body.appendChild(dummy);
                  
                    // Set its ID
                    dummy.setAttribute("id", "dummy_id");
                  
                    // Output the array into it
                    document.getElementById("dummy_id").value=checkbx;
                  
                    // Select it
                    dummy.select();
                  
                    // Copy its contents
                    document.execCommand("copy");
                  
                    // Remove it as its not needed anymore
                    document.body.removeChild(dummy);
                  }
                  

                  以及它的 HTML 调用:

                  And its HTML call:

                  <button id="btn_test" type="button" onclick="getSelectedCheckboxes('ID_of_chkbxs_selected')">Copy</button>
                  

                  这篇关于将 JavaScript 变量的输出复制到剪贴板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:修改 JavaScript 对象的副本会导致原始对象发生变化 下一篇:如何隐藏我的源代码以免被复制

                  相关文章

                • <tfoot id='pm9dh'></tfoot>
                      <i id='pm9dh'><tr id='pm9dh'><dt id='pm9dh'><q id='pm9dh'><span id='pm9dh'><b id='pm9dh'><form id='pm9dh'><ins id='pm9dh'></ins><ul id='pm9dh'></ul><sub id='pm9dh'></sub></form><legend id='pm9dh'></legend><bdo id='pm9dh'><pre id='pm9dh'><center id='pm9dh'></center></pre></bdo></b><th id='pm9dh'></th></span></q></dt></tr></i><div id='pm9dh'><tfoot id='pm9dh'></tfoot><dl id='pm9dh'><fieldset id='pm9dh'></fieldset></dl></div>
                    1. <legend id='pm9dh'><style id='pm9dh'><dir id='pm9dh'><q id='pm9dh'></q></dir></style></legend>
                        <bdo id='pm9dh'></bdo><ul id='pm9dh'></ul>
                    2. <small id='pm9dh'></small><noframes id='pm9dh'>