繁体   English   中英

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

[英]Copying data from one text box to another using Java Script

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

在jQuery中,这样的事情:

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

在onkeypress上调用javascript函数

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

编辑使用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>

你可以轻松使用JQuery:

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

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

你可以实现......

 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> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM