0

Possible Duplicate:
How do you handle oncut, oncopy, and onpaste in jQuery?

$(document).ready(function() {
    $('#Description').bind('keyup', function() {   
        var characterLimit = 350;
        var charactersUsed = $(this).val().length;
        if (charactersUsed > characterLimit) {
            charactersUsed = characterLimit;
            $(this).val($(this).val().substr(0, characterLimit));
            $(this).scrollTop($(this)[0].scrollHeight);
        }
    });
});

http://jsfiddle.net/qCasN/

This is my code. I am trying to paste some content in description box using mouse right click paste, but it is not handled by the code. I don't know where it goes wrong, can anybody help on fixing this for me.

Thanks in advance

4

4 回答 4

0

You should use change or input events instead of keyup. If you don't mind little performance loss you could do:

$('#Description').bind('keyup change input', function () { .. });

http://jsfiddle.net/3HdAd/

于 2012-11-20T10:32:00.217 回答
0

You'll need to use the mouseup event, instead of the keyup event. keyup is for keyboard actions. I am not sure if the page will receive a mouseup event from a context menu, though. (right-click -> "paste" makes you click a "paste" button on a non-DOM element.


Or the events Miszy suggested would be a better choice.

于 2012-11-20T10:32:01.357 回答
0

你可以使用 onpaste 事件来处理这个:-

$("#Description").bind("paste", function(){} );

检查此链接:-您如何在 jQuery 中处理 oncut、oncopy 和 onpaste?

于 2012-11-20T10:33:13.123 回答
0

您可以使用默认的 jquery keyup 事件,如下所示:

var characterLimit = 360; 
$("#Description").keyup(function(){
  if($("#Description").val().length > characterLimit){
    $("#Description").val($("#Description").val().substring(0,characterLimit));
   }
});

或者您可以使用 keydown 事件作为

var characterLimit = 360; 
    $("#Description").keydown(function(){
      if($("#Description").val().length > characterLimit){
        return false;
       }
    });
于 2012-11-20T10:52:49.060 回答