使用 JQuery,您如何检测文本输入的值是否已更改而字段仍然具有焦点? [英] Using JQuery, how do you detect if the value of a text input has changed while the field still has focus?

查看:21
本文介绍了使用 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 事件.

You can just listen for the keypress event.

<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天全站免登陆