[英]How can I save input value and put the value in other input?
I have 2 empty inputs. 我有2个空输入。 I want the user to write something in input 1 and after that the text will show up in input 2.
我希望用户在输入1中编写一些内容,然后文本将显示在输入2中。
Since you didn't tag your question with jQuery, I am assuming that you want to do this just with pure Javascript. 由于您没有使用jQuery标记问题,因此我假设您只想使用纯Javascript进行此操作。
I would recommend using ID attributes on your fields like so: 我建议像这样在您的字段上使用ID属性:
<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. 然后,使用以下Javascript查找元素,然后将keyup事件绑定到第一个输入,以调用一个函数,该函数将值从input1复制到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/ 您可以在此处查看此代码的运行情况: http : //jsfiddle.net/8KptW/1/
To read more about addEventListener
see: https://developer.mozilla.org/en/DOM/element.addEventListener 要了解有关
addEventListener
更多信息,请参见: https : //developer.mozilla.org/en/DOM/element.addEventListener
To read more about getElementByID
see: https://developer.mozilla.org/en/DOM/document.getElementByID 要了解有关
getElementByID
更多信息,请参见: https : //developer.mozilla.org/en/DOM/document.getElementByID
UPDATE UPDATE
Ofcourse... If you are including the jQuery framework, you could use this code: 当然,如果要包括jQuery框架,则可以使用以下代码:
$('#input1').bind('keyup', function () {
$('#input2').val(this.value);
});
Working Example Here: http://jsfiddle.net/8KptW/2/ 此处的工作示例: http : //jsfiddle.net/8KptW/2/
You can read more about jQuery and it's event binding here: http://api.jquery.com/bind 您可以在此处阅读有关jQuery及其事件绑定的更多信息: 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 请注意,它依赖于DOM 2和一些原型语法
使用jQuery: http : //fiddle.jshell.net/Shaz/bwKZt/10/
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.