简体   繁体   English

输入删除/撤消的事件处理程序

[英]event handler for input delete/undo

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
});     

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. 您遇到的问题多于此,您还必须担心具有自动填充功能的浏览器等。因此,HTML5包含了input事件,该事件包含在现代浏览器中。

See this answer for a method of capturing every conceivable change event(*) the browser will let you capture, without firing more than once per change. 请参阅此答案 ,了解捕获每个可能的更改事件(*)的方法,浏览器将允许您捕获,而不会在每次更改时触发多次。

(*) Looks like they forgot cut , so add that in too. (*)看起来他们忘了cut ,所以也加上它。

I'd suggest using keydown and input : http://jsfiddle.net/vKRDR/ 我建议使用keydowninputhttp//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和输入,事件立即发生,而不是等待用户释放按钮或模糊输入。

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无法处理的地方,例如上下文菜单删除/粘贴/剪切

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. 希望,这将适用于您的情况。 :) :)

使用更改事件。

$('#input').on('change', function(){});

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. 我不知道“更改”是否适用于这些操作,我知道当输入元素失去焦点时,通常只会调用更改。

Use this to detect deleting action on input value使用它来检测对输入值的删除操作

document.getElementById("my_id_name").oninput = function(e) {
     if ((e.inputType == 'deleteContentBackward') || (e.inputType == 'deleteContentForward') || (e.inputType == 'deleteByCut')){
            $('my_id_name').value = null
        })
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM