javascript 实时复制另一个文本框值 Jquery
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10698448/
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
Copy another textbox value in real time Jquery
提问by fart-y-goer
I want to get the value of another textbox and input it in realtime into the other textbox. HOW CAN I DETECT IF TEXT_3 WAS CHANGED? IF TEXT_3 VALUE CHANGED, IT MUST BE INPUTTED TO TEXT_4 For your convenience, here is the code and the demo:
我想获取另一个文本框的值并将其实时输入另一个文本框。如何检测 TEXT_3 是否已更改?如果 TEXT_3 VALUE CHANGED,它必须输入到 TEXT_4 为方便起见,这里是代码和演示:
**HTML**
<label>TEXT 1: </label><input type="text" id="text_1" value=""/>
<label>TEXT 2: </label><input type="text" id="text_2" value=""/>
<label>TEXT 3: </label><input type="text" id="text_3" value=""/>
<label>TEXT 4: </label><input type="text" id="text_4" value=""/>
**JAVASCRIPT**
/* INPUT TEXT_1 AND TEXT_2 VALUE TO TEXT_3 ON TEXT_1 KEYUP*/
$("#text_1").keyup(function() {
$("#text_3").val($("#text_1").val() + " " + $("#text_2").val());
})
/* INPUT TEXT_1 AND TEXT_2 VALUE TO TEXT_3 ON TEXT_1 AND TEXT_2 KEYUP*/
$("#text_2").keyup(function(){
$("#text_3").val($("#text_1").val() + " " + $("#text_2").val());
})
/* HOW CAN I DETECT IF TEXT_3 WAS CHANGED? IF TEXT_3 VALUE CHANGED, IT MUST BE INPUTTED TO TEXT_4*/
/* not working solution */
$("#text_3").change(function(){
$("#text_4").val($("#text_3").val());
})
DEMO: http://jsfiddle.net/8eXRx/7/
演示:http: //jsfiddle.net/8eXRx/7/
Thanks for responses!
感谢您的回复!
回答by Chris Li
Add change() after your textbox3.val(), like below:
在 textbox3.val() 之后添加 change(),如下所示:
$("#text_1").keyup(function() {
$("#text_3").val($("#text_1").val() + " " + $("#text_2").val()).change();
})
/* INPUT TEXT_1 AND TEXT_2 VALUE TO TEXT_3 ON TEXT_1 AND TEXT_2 KEYUP*/
$("#text_2").keyup(function(){
$("#text_3").val($("#text_1").val() + " " + $("#text_2").val()).change();
})
回答by VisioN
The problem is that you can't bind special event to check that the textbox value was changed using JavaScript and not manually. To solve the task, one option is to use the same keyup
event for both text_1
and text_2
. JQuery will add the new handler to the existing handlers:
问题是您无法绑定特殊事件来检查文本框值是否已使用 JavaScript 而不是手动更改。为了解决这个任务,一个选择是使用相同的keyup
事件都text_1
和text_2
。JQuery 会将新的处理程序添加到现有的处理程序中:
$("#text_1, #text_2").keyup(function(){
$("#text_4").val($("#text_3").val());
})
回答by Johannes Klau?
the change
event fires after the input field has lost it's focus. If you want to update it in realtime you also need the keyup
event, so something like this:
该change
事件在输入字段失去焦点后触发。如果您想实时更新它,您还需要该keyup
事件,如下所示:
$("#text_3").keyup(function(){
$("#text_4").val($("#text_3").val());
})
回答by Vishal
Try:
尝试:
$("#text_3").keyup(function(){
cur_val = $(this).val(); //grab #text_3's current value
$(this).val(cur_val); // this is optional, but keep for sake
$("#text_4").val(cur_val); //set #text_4's value as what #text_3 is having
});
回答by Subrahmanya Prasad
Try this.. n1 and n2 are ids of textbox
试试这个.. n1 和 n2 是 textbox
$(document).ready(function(){
$(':input').keyup(function(){
$("#n2").val($("#n1").val()).change();
});
});