Javascript 如何使用 jquery 将文本框值分配给另一个文本框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5543072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 17:44:01  来源:igfitidea点击:

How to assign Textbox value to the other textbox using jquery

javascriptjquery

提问by kumar

I have two textboxes in my view,

我有两个文本框,

My requirement is when I enter some text in the first textbox automatically that value should be assigned to second text box when I leave first textbox. using jquery or javascript.

我的要求是当我在第一个文本框中自动输入一些文本时,当我离开第一个文本框时,应该将值分配给第二个文本框。使用 jquery 或 javascript。

thanks for your help

感谢您的帮助

EDIT:

编辑:

For Some reason this code is not inserting the value into seconed textbox.

出于某种原因,此代码未将值插入到第二个文本框中。

http://jsfiddle.net/9tEV2/4/

回答by Chandu

Capture the onchange event of the first textbox and in the event handler assign the first textbox value to second one.

捕获第一个文本框的 onchange 事件,并在事件处理程序中将第一个文本框值分配给第二个。

Something like:

就像是:

<input type="text" id="name"/>
<input type="text" id="anotherName"/>

<script type="text/javascript">
$(function(){
  $("#name").change(function(){
   $("#anotherName").val($(this).val());
  });
})
</script>

回答by Eli

$('#textbox1').blur(function(e) {

    $('#textbox2').val($(this).val());

});

回答by ?ime Vidas

Non-jQuery solution:

非 jQuery 解决方案:

A.onblur = function() { 
    B.value = this.value;
};

Where Aand Bare references to the text-box elements.

其中AB是对文本框元素的引用。

Live demo:http://jsfiddle.net/simevidas/9tEV2/

现场演示:http : //jsfiddle.net/simevidas/9tEV2/

回答by Faber

Hi

你好

try with

尝试

$('#Text1').keypress(function () {
    $('#Text2').val($(this).val());
});

I hope it's helpful

我希望它有帮助

Do not consider this post, I didn't read correctly the question

不要考虑这篇文章,我没有正确阅读问题

回答by Arfat

<script type="text/javascript">
    $(function () {
        $("#txt1").change(function () {

           $("#text2").val($(this).val());// For Single textBox
            $("#filltext").find("input:text").val($(this).val()) // Fill textbox1 value to multiple textboxes present in a single div(filltext is Division ID)
        });
    })
</script>`