Javascript 将一个文本框的内容复制到另一个

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2287144/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 23:37:30  来源:igfitidea点击:

Copy contents of one textbox to another

javascripthtml

提问by Hulk

Suppose an entry is made in a textbox. Is it possible to retain the same entered text in a second text box? If so, how is this done?

假设在文本框中输入了一个条目。是否可以在第二个文本框中保留相同的输入文本?如果是这样,这是如何完成的?

<html>
      <label>First</label>
      <input type="text" name="n1" id="n1">
      <label>Second</label>
      <input type="text" name="n1" id="n1"/>
</html>

回答by Amarghosh

<script>
function sync()
{
  var n1 = document.getElementById('n1');
  var n2 = document.getElementById('n2');
  n2.value = n1.value;
}
</script>
<input type="text" name="n1" id="n1" onkeyup="sync()">
<input type="text" name="n2" id="n2"/>

回答by Ranjit Singh

More efficiently it can be done as : For the one who will see the post now should use best practices of javascript.

更有效的做法是:对于现在将看到该帖子的人来说,应该使用 javascript 的最佳实践。

<script>
function sync(textbox)
{
  document.getElementById('n2').value = textbox.value;
}
</script>
<input type="text" name="n1" id="n1" onkeyup="sync(this)">
<input type="text" name="n2" id="n2"/>

回答by x2.

<html>
<script type="text/javascript">
function copy()
{
    var n1 = document.getElementById("n1");
    var n2 = document.getElementById("n2");
    n2.value = n1.value;
}
</script>
<label>First</label><input type="text" name="n1" id="n1">
<label>Second</label><input type="text" name="n2" id="n2"/>
<input type="button" value="copy" onClick="copy();" />
</html>

回答by James Wiseman

Well, you have two textboxes with the same ID. An Id should be unique, so you should prbably change this.

好吧,您有两个具有相同 ID 的文本框。Id 应该是唯一的,所以你应该改变它。

To set the value from one text box to another a simple call to getElementById()should suffice:

要将一个文本框的值设置到另一个文本框,只需简单调用即可getElementById()

document.getElementById("n1").value=  document.getElementById("n2").value;

(assuming, of course you give your secodn text box an id of n2)

(当然,假设你给你的第二个文本框一个 id n2

Tie this up to a button click to make it work.

将此与按钮单击联系起来以使其工作。

回答by Giovanni

This worked for me and it doesn't use JavaScript:

这对我有用,它不使用 JavaScript:

<form name="theform" action="something" method="something" />
 <input type="text" name="input1" onkeypress="document.theform.input2.value = this.value" />
 <input type="text" name="input2" />
</form>

I found the code here

我在这里找到了代码

回答by Frank Geens

Use event "oninput". This gives a more robust behavior. It will also trigger the copy function when you copy paste.

使用事件“oninput”。这提供了更健壮的行为。复制粘贴时也会触发复制功能。

回答by Udhav Sarvaiya

You can this way also used copy contents of one textbox to another

您也可以通过这种方式将一个文本框的内容复制到另一个

function populateSecondTextBox() {
        document.getElementById('txtSecond').value = document.getElementById('txtFirst').value;
}
<label>Write Here :</label> 
<input type="text" id="txtFirst" onkeyup="populateSecondTextBox();" />
<br>
<label>Will be copied here :</label>
<input type="text" id="txtSecond" />