detect paste on input box

前端 未结 4 1273
挽巷
挽巷 2020-12-14 21:34

I have inputbox. 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 insta

相关标签:
4条回答
  • 2020-12-14 22:02

    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

    0 讨论(0)
  • 2020-12-14 22:09

    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!');
            });
        });
    
    0 讨论(0)
  • 2020-12-14 22:09

    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.

    0 讨论(0)
  • 2020-12-14 22:17

    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 讨论(0)
提交回复
热议问题