【问题标题】:Edit multiple HTML text inputs simultanously同时编辑多个 HTML 文本输入
【发布时间】:2017-08-14 11:33:07
【问题描述】:

我有 2 个 HTML(表单元素)输入,都是文本类型。当我写第一个时,我希望我写的相同文本出现在我写的第二个文本输入中。这可能吗?如何?这在网络上很难找到。

【问题讨论】:

  • 您需要使用 JavaScript 挂钩第一个 input 的输入事件,并将其设置为第二个的值。
  • 将更改事件处理程序添加到第一个输入。在此更新中,第二个。

标签: javascript html forms html-input


【解决方案1】:

document.getElementById('first').onkeyup = function(e) {
  document.getElementById('second').value = e.target.value;
}
<input type="text" id="first" placeholder="Type in this">
<input type="text" id="second">

【讨论】:

  • 这行得通,谢谢!我正在使用 Angular 2,所以我在他们的文档中找到了一个更好/更简单的解决方案,但我要求提供通用 HTML,所以你的答案更合适,并且会更多地帮助其他人:)
【解决方案2】:
<input type="text" id="inputFirst" onchange="mutipleText()">
<input type="text" id="inputSecond" >   

 function mutipleText(){
    var firstInput = $('#inputFirst').val();
    $('#inputSecond').val(firstInput)
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-10
    • 2023-01-26
    • 2012-03-15
    • 2019-06-06
    • 1970-01-01
    相关资源
    最近更新 更多