简体   繁体   English

将剪贴板中的代码粘贴到多个输入字段中

[英]Paste code from clipboard in multiple input fields

I was looking for Vanilla Javascript solution, of copy paste code into multiple input fields.我正在寻找 Vanilla Javascript 解决方案,将粘贴代码复制到多个输入字段中。

i have got solutions on internet but are jQuery based.我在互联网上有解决方案,但基于 jQuery。 This was jQuery Solution 这是 jQuery 解决方案

<input type="text" maxlength="1">
<input type="text" maxlength="1">
<input type="text" maxlength="1">
<input type="text" maxlength="1">
<input type="text" maxlength="1">
<input type="text" maxlength="1">

I had copy this code from email, and past code on any input field, it will paste complete code one by one in each field.我从电子邮件中复制了此代码,并在任何输入字段上复制了过去的代码,它将在每个字段中一一粘贴完整的代码。 Then get this code verify with pre-saved code.然后使用预先保存的代码验证此代码。 Pasting and then collecting and verify code in vanilla JavaScript is what i am looking for.vanilla JavaScript 中粘贴然后收集和验证代码是我正在寻找的。

Here's one way to do it, it can easily be modified to work on specific text inputs, but as-is this ensures every text input on the page gets the same data from the clipboard.这是一种方法,它可以很容易地修改为处理特定的文本输入,但是这样可以确保页面上的每个文本输入都从剪贴板获得相同的数据。

Side note: querySelectorAll returns a nodelist instead of an array, you can use [].forEach.call to use an Array's forEach method in a nodelist .旁注: querySelectorAll返回一个nodelist而不是数组,您可以使用[].forEach.callnodelist使用数组的forEach方法。

 // Listen to paste on the document document.addEventListener("paste", function(e) { // if the target is a text input if (e.target.type === "text") { var data = e.clipboardData.getData('Text'); // split clipboard text into single characters data = data.split(''); // find all other text inputs [].forEach.call(document.querySelectorAll("input[type=text]"), (node, index) => { // And set input value to the relative character node.value = data[index]; }); } });
 <input type="text" maxlength="1"> <input type="text" maxlength="1"> <input type="text" maxlength="1"> <input type="text" maxlength="1"> <input type="text" maxlength="1"> <input type="text" maxlength="1">

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

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