How do I capture the input value on a paste event?

后端 未结 5 1066
清歌不尽
清歌不尽 2020-12-05 10:08

On my site users can paste text (in this case a url) into an input field. I\'d like to capture the value of the text that was pasted using jQuery. I\'ve got this to work in

5条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-05 11:10

    Even better is it to use e.originalEvent.clipboardData.getData('text'); to retrieve pasted data;

    $("input").on("paste", function(e) { 
        var pastedData = e.originalEvent.clipboardData.getData('text');
        // ... now do with pastedData whatever you like ...
    });
    

    This way you can avoid timeouts and it is supported on all major browsers.

提交回复
热议问题