jQuery.keypress:即时文本处理
我需要替换默认的按键事件,以便在您键入时对文本进行额外的(即时)转换。尝试这个:
field.keypress(function(e){
field.val(function(i, val){
return val.toUpperCase();
});
return false;
});
但是输入没有完全填充,这是可以理解的原因。但如何?)
请不要建议“文本转换”,toUpperCase() 只是一个例子。 ))
I need to replace the default keypress-event, for the additional (instant) transformation of the text as you type. Trying this:
field.keypress(function(e){
field.val(function(i, val){
return val.toUpperCase();
});
return false;
});
But input is not filled completely, it is understandable why. But how to?)
Please, don't suggest "text-transform", toUpperCase() is just an example. ))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
keypress
事件在文本字段实际修改之前触发。您应该将该函数绑定到
keyup
事件。不过,这个
keyup
事件每次击键只会触发一次。如果按 a 键,keypress
事件将在每个字符填充文本字段之前触发。keyup
事件仅在释放按键后触发。因此,如果您按住 e 10 秒,输入字段将包含小写e
字符,直到您抬起手指。The
keypress
event is fires before the text field has actually modified.You should bind the function to the
keyup
event.This
keyup
event only fires once per keystroke, though. If you press the a key, thekeypress
event will fire before each character populates the text field. Thekeyup
event is only fired after the key has been released. So, if you hold e for 10 seconds, the input field will contain lowercasee
characters, until you lift your finger.