使用JQuery,如何检测文本输入的值是否在字段仍具有焦点的情况下发生变化? [英] Using JQuery, how do you detect if the value of a text input has changed while the field still has focus?

查看:144
本文介绍了使用JQuery,如何检测文本输入的值是否在字段仍具有焦点的情况下发生变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在发布此问题之前,我注意到以前曾经出现过类似的问题,但是在这种情况下,用户不使用键盘与文本字段进行交互,在这种情况下,将文本字段绑定到粘贴操作或任何其他漂亮的建议在我的情况下将不起作用。

I noticed prior to posting this question that there have been similar questions posted on this topic before, however the user isn't interacting with the text field by using the keyboard in this instance, in such case binding the text field to the "Paste" action or any of the other nifty suggestions wouldn't work in my case.

我们的用户正在输入从条形码扫描的字符串值。我们试图做的是避免在扫描信息后使用户将扫描仪放在下一个字段的烦恼。但是,我仍然有一个问题检测文本字段的价值变化,但仍然有焦点。

Our users are inputting a string value that is scanned in from a Bar Code. What we're attempting to do is to avoid the annoyance of having the user put the scanner down to go to the next field after scanning in the information. However, I'm having an issue detecting the change in value of a text field while it still has focus.

这是我们唯一的谜题的一部分缺少,因为将重点应用到窗体上的下一个字段是微不足道的。当输入设备不是键盘时,任何人都可以了解如何检测文本字段的值的变化?我已经尝试使用change()事件,但是在该字段不再具有焦点之前不会触发。

This is the only part of the puzzle that we're missing, because to apply focus to the next field on the form is trivial. Can anyone shed light on how to detect a change in value of a text field when the input device is NOT the keyboard? I've already tried using the change() event but it doesn't fire until the field no longer has focus.

推荐答案

您可以直接听 keypress 事件。

<input type="text" id="target" value="" />

var target = $('#target'),
    val = target.val();

function monitor()
{
    var current_val = $(this).val();
    if (current_val != val) {
        console.log('changed from', val, 'to', current_val);
        val = current_val;
    }
}

target.keypress(monitor);

这篇关于使用JQuery,如何检测文本输入的值是否在字段仍具有焦点的情况下发生变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆