[英]How To Use JavaScript To Copy Text From One Field To Another
Often when creating a form on a web page, I need our customers to fill out a field such as a permanent address, as well as a present address. 通常,当在网页上创建表单时,我需要我们的客户填写一个字段,例如永久地址和现在的地址。 Instead of having our customers fill out the form twice,I can use JavaScript to copy the form's data from one field to another. 我可以使用JavaScript将表单的数据从一个字段复制到另一个字段,而不是让客户两次填写表单。
try this.... 尝试这个....
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>JS Bin</title> </head> <script> function myFunction(){ console.log(this); document.getElementById('permanentaddress').value = document.getElementById('address').value } </script> <body> Address: <textarea id="address"></textarea><br /> permanent address:<textarea id="permanentaddress" onfocusin="myFunction()"></textarea> </body> </html>
$("#txt2").on("focus",function(){ $("#txt3").val($("#txt1").val()); }); $("#txt2").on("input",function(){ $("#txt3").val(''); var res=$("#txt1").val()+' '+ $(this).val(); $("#txt3").val(res); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" id="txt1" /> <input type="text" id="txt2" /> <input type="text" id="txt3" readonly />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.