我发现textbox上的jQuery更改事件直到我在textbox外部单击时才会触发。
HTML:
<input type="text" id="textbox" />
JS:
$("#textbox").change(function() {alert("Change detected!");});
我的应用程序要求在文本框中的每个字符更改时触发事件。我甚至试着用keyup代替...
$("#textbox").keyup(function() {alert("Keyup detected!");});
...but这是一个众所周知的事实,keyup事件不会在右键单击和粘贴时触发。
有什么解决方法吗?同时拥有两个监听器会造成什么问题吗?
发布于 2013-06-26 18:29:25
绑定到这两个事件是完成此操作的典型方法。还可以绑定到paste事件。
您可以绑定多个事件,如下所示:
$("#textbox").on('change keyup paste', function() {
console.log('I am pretty sure the text box changed');
});
如果你想学究它,你也应该绑定到mouseup以适应文本的拖动,并添加一个lastValue
变量来确保文本确实发生了变化:
var lastValue = '';
$("#textbox").on('change keyup paste mouseup', function() {
if ($(this).val() != lastValue) {
lastValue = $(this).val();
console.log('The text box really changed this time');
}
});
如果你想成为super duper
书呆子,那么你应该使用一个间隔计时器来满足自动填充,插件等的需求:
var lastValue = '';
setInterval(function() {
if ($("#textbox").val() != lastValue) {
lastValue = $("#textbox").val();
console.log('I am definitely sure the text box realy realy changed this time');
}
}, 500);
发布于 2013-06-26 18:30:09
发布于 2014-06-21 01:00:35
$(this).bind('input propertychange', function() {
//your code here
});
这是为打字,粘贴,鼠标右键点击粘贴等工作。
https://stackoverflow.com/questions/17317465
复制相似问题