简体   繁体   English

如何使用JQuery在Android浏览器中禁用“粘贴”?

[英]How to disable 'Paste' in Android browser using JQuery?

I have an input field which I do not want users to paste any values in it. 我有一个输入字段,我不希望用户粘贴其中的任何值。 I am using the following jQuery code and it works fine on desktop Browser and iPhone Safari. 我使用以下jQuery代码,它在桌面浏览器和iPhone Safari上运行良好。 The problem is it's not working on Android browser. 问题是它不适用于Android浏览器。

$('#no_paste').bind("paste", function(e) {                
        e.preventDefault();
});

Here's the fiddle 这是小提琴

I've tested this on Galaxy SIII and Android browser doesn't seem to send the paste event. 我在Galaxy SIII上测试了这个,Android浏览器似乎没有发送paste事件。 However, it still sends the input event after something was pasted. 但是,它仍会在粘贴某些内容后发送input事件。

If user is typing into a field he will fire one input event for each letter. 如果用户在字段中input他将为每个字母触发一个input事件。 However, if he is pasting, input event will fire only once for the whole string that was pasted. 但是,如果他正在粘贴,则input事件将仅对粘贴的整个字符串触发一次。 Basing on this observation we can block pasting like this: 根据这个观察,我们可以阻止粘贴:

$('#ie').bind("input", function() {
    var previousValue = $(this).data('old_value') || '',
        newValue = $(this).val();

    if((newValue.length - previousValue.length) > 1) {
        $(this).val(previousValue);
    }

    $(this).data('old_value', $(this).val());
});

You will find JSFiddle here . 你会在这里找到JSFiddle。

Please note that this will also block autocomplete and all other strange input techniques that work in a similar fashion (I don't know about any). 请注意,这也将阻止自动完成和所有其他以类似方式工作的奇怪输入技术(我不知道任何)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM