Как вставить данные из буфера обмена в несколько полей ввода? - PullRequest
4 голосов
/ 29 сентября 2019

Как выполнить копирование в буфер обмена со многими входами?У меня есть этот код

КОД HTML

<input type="text" value="Hello" id="myInput1">
<input type="text" value="World" id="myInput2">

<button onclick="myFunction()">Copy text</button>

КОД СКРИПТА

<script>
function myFunction() {
  var copyText1 = document.getElementById("myInput1");
  var copyText2 = document.getElementById("myInput1");
  copyText1.select();
  copyText1.setSelectionRange(0, 99999)
  document.execCommand("copy");
  alert("Copied the text: " + copyText.value);
}
</script>

Ответы [ 2 ]

0 голосов
/ 29 сентября 2019

Может быть, это?
для получения дополнительной информации об использовании буфера обмена => https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Interact_with_the_clipboard#Using_the_Clipboard_API

btCopy.onclick=_=>
  {
  // step 1: collect the input values (a method like so many others)
  let inputs = {}
  Array.from(new FormData(myForm),entry=>{inputs[ entry[0] ] = entry[1]})
  let All_inputs = JSON.stringify(inputs)

  // step 2 : write All_inputs into the clipboard
  navigator.clipboard.writeText(All_inputs)

  // as in your wishes...
  alert('Copied the text: ' + All_inputs)
  }
<form id="myForm">
  <input type="text" name="Description1" value="Hello">
  <input type="text" name="Description2" value="world">
</form>

<button id="btCopy">copy</button>
0 голосов
/ 29 сентября 2019

Вы можете добавить третье поле ввода (или textarea, если вы также хотите добавить символ новой строки ) и просто скрыть его.И, непосредственно перед выполнением команд выбора и копирования текста, раскройте текстовую область, а затем снова скройте ее.

function myFunction() {
    var copyText1 = document.getElementById("myInput1");
    var copyText2 = document.getElementById("myInput2");
    var hiddenInput = document.getElementById("hiddenInput");
    hiddenInput.value='Description1: '+copyText1.value+'\nDescription2: '+copyText2.value;
    hiddenInput.style.display='';
    hiddenInput.select();
    hiddenInput.setSelectionRange(0, 99999);
    document.execCommand("copy");
    hiddenInput.style.display='none';
    alert("Copied the text:\n" + hiddenInput.value);
}
<input type="text" value="Hello" id="myInput1">
<input type="text" value="World" id="myInput2">
<textarea id="hiddenInput" style="display:none;"></textarea>
    
<button onclick="myFunction()">Copy text</button>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...