简体   繁体   English

另一个文本框中的一个文本框值

[英]one textbox value in another textbox

I want to get value of one textbox and put the same in another textbox. 我想获取一个文本框的值,然后将其放入另一个文本框。 my code is : 我的代码是:

<input type="text" value="Keyword" id="one" />
<input type="text" value="Search" id="two" />

<a href="#" id="btn">button</a>

jquery: jQuery的:

var input = $("#one");

    $('#btn').click(function(){
        alert('dgdhjdgj');
        var oneValue = $('#one').val();
        alert("one value "+ oneValue);
        var twoVal =  $('#two').val($(input).attr('value'));
         alert('two Val' + twoVal);
     });

demo is here. 演示在这里。

Issue : when I change the value of textbox #one, it does not change the value of #two. 问题:当我更改文本框#one的值时,它不会更改#two的值。 thanks in advance. 提前致谢。

$(input).attr('value') gets the value of the value attribute , which is the initial value, not the current value. $(input).attr('value')获取value属性 ,它是初始值,而不是当前值。

You had it right two lines earlier. 您早两行就对了。 Use val() . 使用val()

Try this 尝试这个

HTML HTML

<input type="text" placeholder="Keyword" id="one" />
<input type="text" placeholder="Search" id="two" />

<a href="#" id="btn">button</a>

Script 脚本

$('#btn').click(function() {
   var oneValue = $('#one').val();
   $('#two').val(oneValue)

})

Fiddle 小提琴

write textarea and check it. 编写textarea并进行检查。 JSFIDDLE 的jsfiddle

$("#add").click(function(){
 var thenVal = $("#textarea_first").val();
 $("#textarea_second").val(thenVal);
});

if all that you want to change the text of second textbox, as soon as you change the text of first textbox, just use jQuery's change event. 如果要更改第二个文本框的文本,则只要更改第一个文本框的文本,就使用jQuery的change事件。

just try this then: 那就试试看吧:

$('#one').on("change",function(){
   $('#two').val($(this).val());
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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