跟踪/观察文本框值已更改 [英] Track/Observe TextBox value changed

查看:112
本文介绍了跟踪/观察文本框值已更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何方法可以检测文本框的值是否发生变化,无论用户是明确更改它还是使用某些Java脚本代码修改了文本框?我需要检测此更改.

Is there is any way to detect text box value changed , whether users changes it explicitly or some java script code modified the text box? I need to detect this change.

推荐答案

要跟踪用户更改,可以为按键添加处理程序:

To track for user changes you can add a handler for key presses:

$(selector).keypress(function() {
    // your code
});

更新:除了观看按键操作外, 您可以使用change函数通过JavaScript观看更改.它不能立即用于用户更改(仅在输入失去焦点后才调用),但是与keypress一起使用,我相信您可以解决所有情况:

Update: besides watching for key presses, you can use the change function to watch from changes via JavaScript. It won't work immediatly for user changes (is only called after the input loses focus), but together with the keypress I believe you cover all cases:

$(selector).change(function() {
    // the same code
});

setTimeout(function() { $(selector).val("changed"); }, 2000); // Will trigger the change

对不起,它似乎也适用于JavaScript,但我误会了... 这个问题,但是,就能解决您的问题(已通过setTimeout测试,并且能够检测到更改).

sorry, it seemed to work for JavaScript too, but I was mistaken... This question, however, will be able to solve your problem (tested with setTimeout, and it was able to detect the change).

我在jsFiddle中发布了示例.有了这个新的watch插件,您不再需要keypresschange:它将适用于键键入,复制/粘贴,JavaScript等.

I posted an example in jsFiddle. With this new watch plugin, you no longer need keypress or change: it will work for key typing, copy/pasting, JavaScript, etc.

这篇关于跟踪/观察文本框值已更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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