Javascript 使用 Java Script 将数据从一个文本框复制到另一个文本框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2923004/
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-23 02:30:38  来源:igfitidea点击:

Copying data from one text box to another using Java Script

javascriptjavascript-events

提问by joe

I want to copy data from one text box to another in html automatically ie., as I edit the first text box the second one should reflect the same spontaneously

我想自动将数据从一个文本框复制到另一个 html 中,即,当我编辑第一个文本框时,第二个文本框应该自发地反映相同

回答by womp

In jQuery, something like this:

在 jQuery 中,是这样的:

$("#txtBox1").keypress(function() {
  $("#txtBox2").val($(this).val());
}

回答by Salil

call javascript function on onkeypresss

在onkeypresss上调用javascript函数

function copy_data(val){
 var a = document.getElementById(val.id).value
 document.getElementById("copy_to").value=a
}

EDITED USE onkeyup or onblur instead

编辑使用 onkeyup 或 onblur 代替

<html>
<head>
    <script>
    function copy_data(val){
     var a = document.getElementById(val.id).value
     document.getElementById("copy_to").value=a
    }    
    </script>
</head>
<body>
<input type="text" name ="a" id="copy_from" onkeyup="copy_data(this)"/>
<input type="text" name ="a" id="copy_to"/>
</body>
</html>

回答by Sarfraz

You do easily with JQuery:

您可以轻松使用 JQuery:

<input type="text" id="box1" />
<input type="text" id="box2" />

<script type="text/javascript">
$(function(){
  $("#box1").keypress(function()
  {
    $("#box2").val($(this).val());
  }
});
</script>

回答by Shoaib Sayyad

You can achieve by this...

你可以通过这个实现...

function FillBilling(f) {
  if(f.billingtoo.checked == true) {
    f.billingname.value = f.shippingname.value;
    f.billingcity.value = f.shippingcity.value;
  }
}
To add more fields, just add to the parameters shown above...like this:
    f.billingstate.value = f.shippingstate.value;
    f.billingzip.value = f.shippingzip.value;
The HTML for the form you will use looks like this:
<b>Mailing Address</b>
<br><br>
<form>
Name:
<input type="text" name="shippingname">
<br>
City:
<input type="text" name="shippingcity">
<br>
<input type="checkbox" name="billingtoo" onclick="FillBilling(this.form)">
<em>Check this box if Billing Address and Mailing Address are the same.</em>
<P>
<b>Billing Address</b>
<br><br>
Name:
<input type="text" name="billingname">
<br>
City:
<input type="text" name="billingcity">
</form>