代码之家  ›  专栏  ›  技术社区  ›  joe

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

  •  2
  • joe  · 技术社区  · 14 年前

    我想在HTML中自动将数据从一个文本框复制到另一个文本框,也就是说,当我编辑第一个文本框时,第二个文本框应自动反映相同的内容。

    4 回复  |  直到 9 年前
        1
  •  5
  •   Balaji Kandasamy    12 年前

    在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>
    
        2
  •  5
  •   womp    14 年前

    在jquery中,如下所示:

    $("#txtBox1").keypress(function() {
      $("#txtBox2").val($(this).val());
    }
    
        3
  •  2
  •   Sarfraz    14 年前

    使用jquery很容易:

    <input type="text" id="box1" />
    <input type="text" id="box2" />
    
    <script type="text/javascript">
    $(function(){
      $("#box1").keypress(function()
      {
        $("#box2").val($(this).val());
      }
    });
    </script>
    
        4
  •  -1
  •   Shoaib Sayyad    9 年前

    你可以通过这个…

    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>