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

时间:2019-01-30 10:17:56

标签: javascript jquery css

我有在选择一个下拉其中欲复制下拉的值并粘贴到输入字段下面不使用CLT + V每次

这是我尝试的代码:

$('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");
            });

    });

});

在单击具有id changePattern的输入时,我打开一个下拉列表,从中填充id = reg的另一个字段

我正在将模式保存到:

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

1 个答案:

答案 0 :(得分:1)

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

.on()的{​​{1}}更改将其选择的值存储到隐藏的select中。然后将input s注册到click事件。每当在这些input上单击时,就会将隐藏的input的值粘贴到其中。

您得到input来从事.execCommand()的工作吗? input适用于document.execCommand()个元素,而不适用于contenteditable个元素。


演示

form
$('select').on('change', function(event) {

  $('#X').val($(this).val());

});

$('input').on('click', function(event) {

  if ($('#X').val() !== null) {

    $(this).val($('#X').val());

  }
});