jQuery.keypress:即时文本处理

发布于 2024-12-27 08:56:14 字数 277 浏览 1 评论 0原文

我需要替换默认的按键事件,以便在您键入时对文本进行额外的(即时)转换。尝试这个:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

舂唻埖巳落 2025-01-03 08:56:14

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, the keypress event will fire before each character populates the text field. The keyup event is only fired after the key has been released. So, if you hold e for 10 seconds, the input field will contain lowercase e characters, until you lift your finger.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文