简体   繁体   English

如何保存输入值并将其放入其他输入中?

[英]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和一些原型语法

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.

相关问题 我怎样才能得到这个输入 id 值的这个值(这个值是通过 ID 从其他输入值加载的) - how can i get this value of this input id value (this value was loaded from other input value by ID ) 如何使用jQuery将json结果放入输入值中? - How can i put json result into an input value using jQuery? 如何获取输入值并将其放入angularJS中的变量? - How can I take a input value and put into a variable in angularJS? 单击链接后如何在输入中保存价值? - How can I save value in input after click on link? 如何将多个输入框的值保存到本地存储? - How can I save the value of multiple input boxes to local storage? 如何将src的值输入输入然后在另一页上显示它? - How to put the value of src to an input then display it on the other page? 如何给输入标签赋值 - How to put value to the input tag 如何将输入值1放入输入2中,使用jquery - how to put an input value 1 in an input 2 use jquery 如何在不提交表格的情况下获取输入框值并将计算值放入其他输入框? - How to get input box value without submitting form and put calculated value in other input box? 如何从内联样式属性中获取值并将其放入最接近的输入值中? jQuery的 - How can I get a value from an inline style attribute and put inside the closest input value? jQuery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM