Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用JQuery捕获粘贴到textarea中的文本_Jquery_Copy Paste_Jquery Events - Fatal编程技术网

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

使用JQuery捕获粘贴到textarea中的文本,jquery,copy-paste,jquery-events,Jquery,Copy Paste,Jquery Events,我必须使用JQuery获取文本区域的粘贴事件。我尝试了以下代码,但它不工作 $(document).ready(function() { $('#txtcomplaint').keyup(function() { TextCounter('txtcomplaint','counterComplaint', 1000 ); }) $('#txtcomplaint').onpaste(function() { alert() //Tex

我必须使用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);
});

我终于在1)键入、2)拖放、3)Ctrl-V和4)从鼠标单击的上下文菜单粘贴时实现了这一点,但我必须将粘贴和拖放处理程序附加到文档(其中“taValue”是我尝试监视的文本区域的类):

textarea上的keyup事件已起作用。下一个问题是,在textarea中的文本实际更改之前,会触发粘贴和删除事件。在我的例子中,我想将新文本与原始文本进行比较。我求助于设置超时:

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

可能更改不是必需的。

使用此方法无法获取复制的文本。@rahul:他只想使用此事件对海豚进行文本计数。
    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]
$("#item_name").bind("input change", function() {});