使用JQuery捕获粘贴到textarea中的文本

我必须使用JQuery获取文本区域的粘贴事件。 我尝试了以下代码,但它不起作用……

$(document).ready(function() { $('#txtcomplaint').keyup(function() { TextCounter('txtcomplaint','counterComplaint', 1000 ); }) $('#txtcomplaint').onpaste(function() { alert() //TextCounter('txtcomplaint','counterComplaint', 1000 ); }) }); 

你可以做这样的事情

 $("#txtcomplaint").bind('paste', function(e) { var elem = $(this); setTimeout(function() { // gets the copied text after a specified time (100 milliseconds) var text = elem.val(); }, 100); }); 
 $('#txtcomplaint').bind('paste', function(e){ alert('pasting!') }); 

有关其他资源,请查看此处 。

我终于得到了这个工作1)键入,2)拖放,3)Ctrl-V和4)从鼠标单击的上下文菜单粘贴,但我不得不将粘贴和拖放处理程序附加到文档(其中’taValue’是我试图监控的textareas类:

  $(document).on("paste drop", '.taValue', function (e) { myHandler.call(e.target, e); }); 

textarea上的keyup事件已经奏效。 接下来的问题是,在textarea中的文本实际发生更改之前,粘贴和放置事件会被触发。 在我的情况下,我想将新文本与原始文本进行比较。 我求助于一个setTimeout:

  function myHandler(e) { if (e && (e.type === "drop" || e.type === "paste")) { var me = this; setTimeout(function () { myHandler.call(me) }, 200); }... [more code to do the comparison] 

我讨厌对这样的事情使用超时但它确实有效(当我尝试100毫秒间隔时,它没有)。

这是最有用的解决方案:

 $("#item_name").bind("input change", function() {}); 

也许改变不是必不可少的。