jQuery onChange 将字段值添加到 HTML
我想将输入字段的值实时打印到 DIV 中。
现在我明白了:
$("#page_name").change(function () {
var new_val = $("#page_name").val();
$("#page_url").html(new_val);
});
显然,它不起作用。有人有建议吗?
谢谢!
I want to print the value of a input field real-time to a DIV.
Right now I have got this:
$("#page_name").change(function () {
var new_val = $("#page_name").val();
$("#page_url").html(new_val);
});
It doesn't work, obviously. Anyone got a suggestion?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如所指出的,“更改”仅在元素失去焦点时发生。将其绑定到按键事件的另一种方法是将其绑定到“输入”事件。
http://jsfiddle.net/xY7Q6/
按键的问题是它不适用于复制和粘贴文本。
输入事件几乎是每个人都期望的“更改”(o:我认为这个事件是相当新的(HTML5?),较旧的浏览器可能无法获得该事件。
As pointed out 'change' only occurs when the element loses focus. An alternative to binding it to the keypress event would be binding it to the "input" event.
http://jsfiddle.net/xY7Q6/
The problem with keypress is that it does not work on copy&paste text.
The input event is pretty much what everybody expects 'change' to be (o: I think this event is pretty new (HTML5?), older browsers may not get that one.
这应该确实有效。我最好的猜测是:
#page_name
。That should actually work. My best guess is this:
#page_name
that second time.实际上它会起作用,但只有当您将光标移出输入框时,因为当焦点丢失时会触发更改。
你需要的是按键。 相同
代码与您的代码
http://jsfiddle.net/y3zpP/
Actually it will work, but only when you move the cursor out of the input box, because change fires when the focus is lost.
What you need is keypress. The code is just the same as yours
test it here
http://jsfiddle.net/y3zpP/