jQuery 如何使用jQuery实时显示输入值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1403776/
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
How to real time display input value with jQuery?
提问by omg
<input type="text" id="name" />
<span id="display"></span>
So that when user enter something inside "#name",will show it in "#display"
这样当用户在“#name”中输入内容时,将在“#display”中显示
回答by CMS
回答by Felix
A realtime fancy solution for jquery >= 1.9
jquery >= 1.9 的实时解决方案
$("#input-id").on("change keyup paste", function(){
dosomething();
})
if you also want to detect "click" event, just:
如果您还想检测“点击”事件,只需:
$("#input-id").on("change keyup paste click", function(){
dosomething();
})
if your jquery <=1.4, just use "live" instead of "on".
如果您的 jquery <=1.4,只需使用“live”而不是“on”。
回答by janoliver
$('#name').keyup(function() {
$('#display').text($(this).val());
});
回答by Jan Zich
The previous answers are, of course, correct. I would only add that you may want to prefer to use the keydown
event because the changes will appear sooner:
前面的答案当然是正确的。我只想补充一点,您可能更喜欢使用该keydown
事件,因为更改会更快出现:
$('#name').keydown(function() {
$('#display').text($(this).val());
});