简体   繁体   中英

How can I save input value and put the value in other input?

I have 2 empty inputs. I want the user to write something in input 1 and after that the text will show up in input 2.

Since you didn't tag your question with jQuery, I am assuming that you want to do this just with pure Javascript.

I would recommend using ID attributes on your fields like so:

<input id="input1" value="">
<input id="input2" value="">

Then, use the following Javascript to find the elements and then bind a keyup event to the first input to call a function that will perform the copying of the value from input1 to input2.

var input1 = document.getElementById('input1');
var input2 = document.getElementById('input2');

var updateInputs = function () {
    input2.value = input1.value;
}

if (input1.addEventListener) {
    input1.addEventListener('keyup', function () {
        updateInputs();
    });
} else if (input1.attachEvent) { // support IE
    input1.attachEvent('onkeyup', function () {
        updateInputs();
    });
}

You can see this code in action here: http://jsfiddle.net/8KptW/1/

To read more about addEventListener see: https://developer.mozilla.org/en/DOM/element.addEventListener

To read more about getElementByID see: https://developer.mozilla.org/en/DOM/document.getElementByID

UPDATE

Ofcourse... If you are including the jQuery framework, you could use this code:

$('#input1').bind('keyup', function () {
    $('#input2').val(this.value);
});

Working Example Here: http://jsfiddle.net/8KptW/2/

You can read more about jQuery and it's event binding here: http://api.jquery.com/bind

This is my implementation of that goal:

function syncFields(from,to){
    if(!$(from) || !$(to) || $(to).value.length>0){return false;}
    if($(from).nodeName.toLowerCase()=='input' && $(from).type=='text'){
        var etype='keyup';
    }else if($(from).nodeName.toLowerCase()=='select'){
        var etype='change';
    }else{
        return false;
    }
    $(from).addEventListener(etype,sync,false);
    $(to).addEventListener(etype,function(){$(from).removeEventListener(etype,sync,false);},false);
    function sync(){
        if($(to).nodeName.toLowerCase()=='input' && $(to).type=='text'){
            $(to).value=$(from).value;
        }else if($(to).nodeName.toLowerCase()=='select'){
            $(to).selectedIndex=$(from).selectedIndex;
        }
        return true;
    }
}

Note that it relies on DOM 2 and a dash of Prototype Syntax

This will do the trick.

 <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
         <script type="text/javascript">
             function f(a, b) {
                 document.getElementById(a).value = document.getElementById(b).value;
             }
        </script>
    </head>
    <body>

            <input id="Submit1" onkeyup="f('Submit2','Submit1')" type="text"  value="" />
            <input id="Submit2" type="text" value="" />
    </body>
    </html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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