14

我必须使用 JQuery 来处理文本区域的粘贴事件。我已经尝试了以下代码,但它不工作......

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

4 回答 4

27

你可以做这样的事情

$("#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);
});
于 2010-02-11T05:33:13.390 回答
6
$('#txtcomplaint').bind('paste', function(e){ alert('pasting!') });

如需其他资源,请查看此处

于 2010-02-11T05:20:41.263 回答
2

我终于让它适用于 1) 键入、2) 拖放、3) Ctrl-V 和 4) 从鼠标单击的上下文菜单中粘贴,但我必须将粘贴和拖放处理程序附加到文档(其中'taValue' 是我要监控的文本区域的类):

        $(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 毫秒的间隔时,它没有)。

于 2015-04-09T19:59:03.930 回答
1

这是最有用的解决方案:

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

也许改变不是必需的。

于 2017-02-09T00:56:18.707 回答