开发者

detect paste on input box

开发者 https://www.devze.com 2022-12-09 15:42 出处:网络
I have input开发者_StackOverflow中文版box. When page load, i use mouse to right click inputbox and choose paste from contextmenu.

I have input开发者_StackOverflow中文版box. When page load, i use mouse to right click inputbox and choose paste from contextmenu.

when text get pasted, which event to use to alert text instantly as soon as paste happens?

i use "input paste" but not work in IE


You can bind these events like so:

    $(document).ready(function() {
        $("#Text1").bind('copy', function(e) {
            alert('copying text!');
        });
        $("#Text1").bind('paste', function(e) {
            alert('pasting text!');
        });
        $("#Text1").bind('cut', function(e) {
            alert('cut text!');
        });
    });


A hack that would work most of the time would be to hook into the control's onchange while also storing the control's initial text in a separate variable. Any time the length of the new text is longer than the original text by more than one character, you can assume that something was pasted in. Obviously this wouldn't work if someone pasted in a one-character string, but people don't do that very often.


There is a better way now. Still not all browsers support this 2011, but this will change

oninput event handler

oninput fixes

more fixes

and other crossbrowser jquery solutions


It was set an action with setInterval (javascript function), which checks every 200ms the content of input. If it is changed then the past or typing occurred.

0

精彩评论

暂无评论...
验证码 换一张
取 消