How do I capture the input value on a paste event?

后端 未结 5 1065
清歌不尽
清歌不尽 2020-12-05 10:08

On my site users can paste text (in this case a url) into an input field. I\'d like to capture the value of the text that was pasted using jQuery. I\'ve got this to work in

5条回答
  •  失恋的感觉
    2020-12-05 10:59

    Listen for the change event as well as paste. change will reliably fire on a changed field before submission, whereas paste only happens on browsers that support it on an explicit paste; it won't be triggered by other editing actions such as drag-and-drop, cut-copy, undo-redo, spellcheck, IME substitution etc.

    The problem with change is that it doesn't fire straight away, only when editing in a field is finished. If you want to catch all changes as they happen, the event would be input... except that this is a new HTML5 feature that isn't supported everywhere (notably: IE<9). You could nearly do it by catching all these events:

    $('.url').bind('input change paste keyup mouseup',function(e){
        ...
    });
    

    But if you want to definitely catch every change quickly on browsers that don't support input, you have no choice but to poll the value on a setInterval.

提交回复
热议问题