当用户在文本框中键入值时,在另一个文本框中显示一个文本框值

问题描述:

当用户在文本框中键入值时,有没有办法从一个文本框中获取值并使用jQuery动态添加到另一个文本框?如果有任何此类事情,有人可以解释一下这个方法吗?

Is there a way to get the value from one text box and add it to another using jQuery dynamically when user is typing the value to the text box? can someone please explain the method if there is any such thing?

regrds,
Rangana

regrds, Rangana

你的意思是像 http://jsfiddle.net/ZLr9N/

$('#first').keyup(function(){
    $('#second').val(this.value);
});

实际上它非常简单。首先,我们在第一个输入上附加 keyup 事件处理程序。这意味着只要有人在第一个输入中键入内容,就会调用 keyup()函数内的函数。然后我们将第一个输入的值复制到第二个输入,其中 val ()功能。就是这样!

Its really simple, actually. First we attach a keyup event handler on the first input. This means that whenever somebody types something into the first input, the function inside the keyup() function is called. Then we copy over the value of the first input into the second input, with the val() function. That's it!