![](/img/trans.png)
[英]AngularJs - How can i fetch the value from one input field into another field using angular.copy?
[英]How to Copy Value from one Input Field to Another Using JS
我有两个输入字段:
<input type="text" id="one" name="one" />
<input type="text" id="two" name="two" />
我想要一个 function 在不使用 Jquery 的情况下单击第二个输入时自动复制第一个输入的文本
谢谢
这样做的基本方法:
<input type="text" id="one" name="one"> <input type="text" id="two" name="two" onfocus="this.value = document.getElementById('one').value">
要检查用户是否单击了<input>
元素,请为其添加一个事件侦听器。
然后,使用value
属性获取第一个文本字段的值。
这是您的代码:
document.getElementById('two').addEventListener("click", function() { this.value = document.getElementById('one').value; });
<input type="text" id="one" name="one" /> <input type="text" id="two" name="two" />
这是一个现场演示: https://codepen.io/marchmello/pen/XWmezNV?editors=1010
这是执行此操作的示例。
var one = document.getElementById("one"); var two = document.getElementById("two"); function myFunction(){ two.value = one.value; }
<input type="text" id="one" name="one" /> <input type="text" id="two" name="two" onfocus="myFunction()" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.