javascript 输入删除/撤消的事件处理程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14990886/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
event handler for input delete/undo
提问by user2014429
I need to check for all events which will change the contents of my text input. so far I have handlers for keyup, cut and paste. but the content can also be changed by highlighting the text and clicking delete or undo. is there a way to listen for these events?
我需要检查所有会改变我的文本输入内容的事件。到目前为止,我有用于键盘输入、剪切和粘贴的处理程序。但也可以通过突出显示文本并单击删除或撤消来更改内容。有没有办法监听这些事件?
$('#input').on('paste cut keyup ',function() {
//add delete and undo to listner
});
采纳答案by Plynx
You have more problems than this, you also have to worry about browsers with autofill features, etc. For this reason HTML5 has included the input
event, which is included in modern browsers.
您有更多的问题,您还必须担心具有自动填充功能的浏览器等。因此,HTML5input
包含了现代浏览器中包含的事件。
See this answerfor a method of capturing every conceivable change event(*) the browser will letyou capture, without firing more than once per change.
有关捕获浏览器允许您捕获的每个可能的更改事件 (*) 的方法,请参阅此答案,每次更改不会触发多次。
(*) Looks like they forgot cut
, so add that in too.
(*) 看起来他们忘记了cut
,所以也加进去。
回答by Kevin B
I'd suggest using keydown
and input
: http://jsfiddle.net/vKRDR/
我建议使用keydown
和input
:http: //jsfiddle.net/vKRDR/
var timer;
$("#input").on("keydown input", function(){
var self = this;
clearTimeout(timer)
// prevent event from happening twice
timer = setTimeout(function(){
console.log(self.value);
},0);
});
keyup won't catch special keys such as ctrl, and input will catch things such as paste delete etc, even from context menu.
keyup 不会捕获特殊键,例如 ctrl,输入会捕获诸如粘贴删除等内容,即使是从上下文菜单中也是如此。
by using keydown and input, the event happens immediately rather than waiting for the user to release the button or blur the input.
通过使用 keydown 和 input,事件立即发生,而不是等待用户释放按钮或模糊输入。
keydown does most of the work, input picks up where keydown can't handle it such as the context menu delete/paste/cut
keydown 完成了大部分工作,输入会在 keydown 无法处理的地方进行选择,例如上下文菜单删除/粘贴/剪切
回答by Annie Javed
use these, they will get all the changes :
使用这些,他们将获得所有更改:
$(container).bind('keyup input propertychange paste change'), function (e){});
also, you could do one thing to check the value inside this onchange event:
此外,您可以做一件事来检查此 onchange 事件中的值:
$(function(){
$('input').on('keyup input propertychange paste change', function(){
if ($(this).data('val')!=this.value) {
alert('Something has been changed in the input.');
}
$(this).data('val', this.value);
});
});
hopefully, this will work in your case. :)
希望这将适用于您的情况。:)
回答by Iain
Use the change event.
使用更改事件。
$('#input').on('change', function(){});
$('#input').on('change', function(){});
回答by Master Yogurt
You're at least missing keypress and change. I don't know if "change" works with those actions, I do know that change is often just called when the input element loses focus.
您至少缺少按键和更改。我不知道“更改”是否适用于这些操作,我知道更改通常只是在输入元素失去焦点时才被调用。