我有一个用户名和密码字段,我需要从上面的多个文本输入框捕获的两个输入,并合并到用户名和密码字段,这意味着用户名和密码将是相同的.但我的挑战是合并来自多个字段的输入,这就是我到目前为止成功做到的

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.3.1/dist/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>


<style>
    .flex{
        display: flex;
    }
    .otSc {
        margin: 0;
        margin-right: 16px;
        border: 1px solid;
        border-radius: 8px;
        padding: 1px 0px;
        font-size: 15px;
        text-align: center;
        width: 100%;
        outline: none;
    }
    .otSc:last-child{
        margin-right: 0;
    }
    .otpCont {
        padding: 10px 10px;
    }
</style>

<div class="col-md-3">
    <div class="otpCont flex spaceBetween">
        <input id="maxCode" class="otSc" type="text" maxlength="1">
        <input class="otSc form-control" type="text" maxlength="1">
        <input class="otSc form-control" type="text" maxlength="1">
        <input class="otSc form-control" type="text" maxlength="1">
        <input class="otSc form-control" type="text" maxlength="1">
        <input class="otSc form-control" type="text" maxlength="1">
    </div>

    <p>what is typed at the top should be a merged output at the bottom</p>
    <div class="col-md-6 m-b-15" >
        <input id="maxVoucherCode" type="text" class="form-control" name="username" placeholder="user merged input"/>
    </div>
    <div class="col-md-6 m-b-15" >
        <input id="maxVoucherCode" type="text" class="form-control" name="password" placeholder="pass merged input"/>
    </div>
</div>
<script type="text/javascript">
    $("#maxVoucherCode").keyup(function() {
        var maxVoucherCode = $(this).val();
        $("#maxCode").val(maxVoucherCode);    
    });

    $("#maxCode").keyup(function() {
        var maxCode = $(this).val();
        $("#maxVoucherCode").val(maxCode);    
    });
</script>

<script type="text/javascript">
    document.querySelectorAll(".otSc").forEach(function(otpEl) {
        otpEl.addEventListener("keyup", backSp);
        otpEl.addEventListener("keypress", function() {
            var nexEl = this.nextElementSibling;
            nexEl.focus();
        })
    })

    function backSp(backKey) {
        if (backKey.keyCode == 8) {
            var prev = this.previousElementSibling.focus()
        }
    } 
</script>

推荐答案

您不需要jQuery.我将borrow 以下答案中的代码:Paste PIN into multiple input fields,对于将PIN复制到两个隐藏输入(密码/用户名)中,我将另外使用const PINValue = elsInput.reduce((s, el) => s + el.value, "");

示例:

// DOM utility functions:
const els = (sel, par = document) => par.querySelectorAll(sel);
const el = (sel, par = document) => par.querySelector(sel);


// Task: multiple inputs "field"

const elUN = el(`[name="username"]`);
const elPW = el(`[name="password"]`);

// Not sure why you would need this since are hidden, but there you go:
// elUN.addEventListener("input", () => { elPW.value = elUN.value; });
// elPW.addEventListener("input", () => { elUP.value = elPW.value; });


els(".pin").forEach((elGroup) => {

  const elsInput = [...els(`[name="pin[]"]`, elGroup)];
  const len = elsInput.length;
  
  const handlePaste = (ev) => {
    const clip = ev.clipboardData.getData('text');     // Get clipboard data
    const pin = clip.replace(/\s|-/g, "");             // Sanitize string
    const ch = [...pin];                               // Create array of chars
    elsInput.forEach((el, i) => el.value = ch[i]??""); // Populate inputs
    elsInput[Math.min(len, pin.length) - 1]?.focus();  // Focus input
  };

  const handleInput = (ev) => {
    const elInp = ev.currentTarget;
    const i = elsInput.indexOf(elInp);
    if (elInp.value && (i+1) % len) elsInput[i + 1]?.focus();  // focus next
    
    // Copy to UN/PW:
    const PINValue = elsInput.reduce((s, el) => s + el.value, "");
    elUN.value = PINValue;
    elPW.value = PINValue;
  };
  
  const handleKeyDn = (ev) => {
    const elInp = ev.currentTarget
    const i = elsInput.indexOf(elInp);
    if (!elInp.value && ev.key === "Backspace" && i) elsInput[i - 1]?.focus(); // Focus previous
  };
  
  
  // Add the same events to every input in group:
  elsInput.forEach(elInp => {
    elInp.addEventListener("paste", handlePaste);   // Handle pasting
    elInp.addEventListener("input", handleInput);   // Handle typing
    elInp.addEventListener("keydown", handleKeyDn); // Handle deleting
  });
  
});
    
.flex {
  display: flex;
}

.pin>input {
  text-align: center;
  width: 2rem;
  height: 2rem;
}
Copy some of thise PINs into any of the below 6 PIN inputs:<br>
<code>123329</code><br>
<code>6 5 0 3 2 1</code><br>
<code>444-998</code><br>
<code>x1A23z</code>
<code>12</code>
<br>

<div class="col-md-3">
  <div class="otpCont flex spaceBetween">

    <form>
      <h3>PIN:</h3>
      <div class="pin">
        <input type="text" name="pin[]" maxlength="1" autocomplete="off" required autofocus>
        <input type="text" name="pin[]" maxlength="1" autocomplete="off" required>
        <input type="text" name="pin[]" maxlength="1" autocomplete="off" required>
        <input type="text" name="pin[]" maxlength="1" autocomplete="off" required>
        <input type="text" name="pin[]" maxlength="1" autocomplete="off" required>
        <input type="text" name="pin[]" maxlength="1" autocomplete="off" required>
      </div>

      <br> Hidden inputs demo:<br>
      <input type="text" name="username">
      <input type="text" name="password">

      <button type="submit" class="btn btn-lg btn-primary">SUBMIT</button>
    </form>
  </div>

</div>

Javascript相关问答推荐

React Hooks中useState的同步问题

为什么从liveWire info js代码传递数组我出现错误?

Phaser框架-将子对象附加到Actor

你怎么看啦啦队的回应?

可更改语言的搜索栏

这个值总是返回未定义的-Reaction

在HTML语言中调用外部JavaScript文件中的函数

当输入字段无效时,我的应用程序不会返回错误

VSCode中出现随机行

SPAN不会在点击时关闭模式,尽管它们可以发送日志(log)等

处理TypeScrip Vue组件未初始化的react 对象

Django导入问题,无法导入我的应用程序,但我已在设置中安装了它

如果NetSuite中为空,则限制筛选

在GraphQL解析器中修改上下文值

在ChartJS中使用spanGaps时,是否获取空值的坐标?

表单数据中未定义的数组键

是否有静态版本的`instanceof`?

AstroJS混合模式服务器终结点返回404

使用Java脚本在div中创建新的span标记

与find()方法一起使用时,Mongoose中的$or运算符没有提供所有必需的数据