Copy contents of one textbox to another

后端 未结 7 967
梦毁少年i
梦毁少年i 2021-02-02 00:38

Suppose an entry is made in a textbox. Is it possible to retain the same entered text in a second text box? If so, how is this done?


      
相关标签:
7条回答
  • 2021-02-02 01:04
    <html>
    <script type="text/javascript">
    function copy()
    {
        var n1 = document.getElementById("n1");
        var n2 = document.getElementById("n2");
        n2.value = n1.value;
    }
    </script>
    <label>First</label><input type="text" name="n1" id="n1">
    <label>Second</label><input type="text" name="n2" id="n2"/>
    <input type="button" value="copy" onClick="copy();" />
    </html>
    
    0 讨论(0)
  • 2021-02-02 01:04

    Use event "oninput". This gives a more robust behavior. It will also trigger the copy function when you copy paste.

    0 讨论(0)
  • 2021-02-02 01:20

    Well, you have two textboxes with the same ID. An Id should be unique, so you should prbably change this.

    To set the value from one text box to another a simple call to getElementById() should suffice:

    document.getElementById("n1").value=  document.getElementById("n2").value;
    

    (assuming, of course you give your secodn text box an id of n2)

    Tie this up to a button click to make it work.

    0 讨论(0)
  • 2021-02-02 01:24
    <script>
    function sync()
    {
      var n1 = document.getElementById('n1');
      var n2 = document.getElementById('n2');
      n2.value = n1.value;
    }
    </script>
    <input type="text" name="n1" id="n1" onkeyup="sync()">
    <input type="text" name="n2" id="n2"/>
    
    0 讨论(0)
  • 2021-02-02 01:25

    You can this way also used copy contents of one textbox to another

    function populateSecondTextBox() {
            document.getElementById('txtSecond').value = document.getElementById('txtFirst').value;
    }
    <label>Write Here :</label> 
    <input type="text" id="txtFirst" onkeyup="populateSecondTextBox();" />
    <br>
    <label>Will be copied here :</label>
    <input type="text" id="txtSecond" />

    0 讨论(0)
  • 2021-02-02 01:26

    This worked for me and it doesn't use JavaScript:

    <form name="theform" action="something" method="something" />
     <input type="text" name="input1" onkeypress="document.theform.input2.value = this.value" />
     <input type="text" name="input2" />
    </form>
    

    I found the code here

    0 讨论(0)
提交回复
热议问题