简体   繁体   English

jQuery 使用 document.execCommand("copy") 和 ("paste") 复制和粘贴

[英]jQuery copy and paste using document.execCommand("copy") and ("paste")

I have one dropdown upon selection of which I want to copy the value of that dropdown and paste in an input field below without using clt+v every time我有一个下拉列表,我想复制该下拉列表的值并粘贴到下面的输入字段中,而无需每次都使用 clt+v

Here is the code what I tried:这是我尝试过的代码:

$('body').append(`<div id="selectDialog" title="Select Regex Type" style="text-align:center;display:none;">
          <select id="listSelection">
            <option value ="">None</option>
            <option value ="[a-z]+">Single Digit Integers</option>
            <option value ="^[0-9]">Multi Digit Number</option>
            <option value ="/^-?[0-9]+\.?[0-9]*$/">Decimal Number</option>
          </select>

          <div style="margin:10px">
                 <button id="closeSelection" style="background- 
                                 color:#3B5E9E" >save</button>
          </div>

 </div>`);



$(function () {

            $("#selectDialog").dialog({
                autoOpen: false,
            });


            $('#changePattern').on("click", function () {
                $("#selectDialog").dialog("open");
            });


            $("#listSelection")
                .change(function () {

                    var s = $("#listSelection option:selected").val();
                    $("#changePattern").val(s);
                    $("#changePattern").attr("patternMask" , s);
                    $('#changePattern').select();
                     document.execCommand("copy");
                     $('#reg').select();

                })
                .trigger("change");

         $('#reg').select( function (){

         /*here I am trying to copy using document.texecCommand("copy");
but unable to copy*/
         });


            $('#closeSelection').on("click", function () {
                $("#selectDialog").dialog("close");
            });

    });

});

on clicking input having an id changePattern , i am opening an dropdown from which i am populating another field with id =reg单击具有 id changePattern 的输入时,我将打开一个下拉列表,从中我将使用 id =reg 填充另一个字段

hi am saving a pattern to:嗨,正在将模式保存到:

<input id="changePattern" />
<input id="reg" />

<input type="hidden"> or <input hidden> <input type="hidden"><input hidden>

.on() change of select store it's selected value to a hidden input . .on()更改select将其选定值存储到隐藏input then register the input s to the click event.然后将input注册到 click 事件。 Whenever a click happens on those input s the value of the hidden input will be pasted to it.每当点击这些input时,隐藏input的值将被粘贴到其中。

Did you get an .execCommand() to work on an input ?你有一个.execCommand()来处理input吗? document.execCommand() are for contenteditable elements not form elements. document.execCommand()用于contenteditable元素而不是form元素。


Demo演示

 $('select').on('change', function(event) { $('#X').val($(this).val()); }); $('input').on('click', function(event) { if ($('#X').val() !== null) { $(this).val($('#X').val()); } });
 <input id='X' hidden value=null> <select> <option value=''></option> <option value='1'>1</option> <option value='2'>2</option> <option value='3'>3</option> </select> <br><br> <input> <input> <input> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

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

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