jQuery 限制可以在 textarea 中粘贴多少个字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2190420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 12:57:49  来源:igfitidea点击:

limit how many characters can be pasted in textarea

jqueryhtmltextareacopy-paste

提问by hoju

Is it possible to detect how many characters are being pasted into a HTML textarea, and cancel the paste if beyond a limit?

是否可以检测有多少字符被粘贴到 HTML 文本区域中,并在超出限制时取消粘贴?

Edit: what I am trying to do is prevent the user pasting a massive amount of characters (~3 million) because it crashes some browsers. So I want to cancel the paste before their browser locks up. I am making a document editor where users are likely to try this. But they can type as much as they want.

编辑:我想要做的是防止用户粘贴大量字符(约 300 万),因为它会使某些浏览器崩溃。所以我想在他们的浏览器锁定之前取消粘贴。我正在制作一个用户可能会尝试的文档编辑器。但是他们可以输入任意数量的内容。

采纳答案by Reigel

you can do this on jQuery like this:

您可以像这样在 jQuery 上执行此操作:

$(document).ready(function(){
function limits(obj, limit){

    var text = $(obj).val(); 
    var length = text.length;
    if(length > limit){
       $(obj).val(text.substr(0,limit));
     } else { // alert the user of the remaining char. I do alert here, but you can do any other thing you like
      alert(limit -length+ " characters remaining!");
     }
 }


$('textarea').keyup(function(){

    limits($(this), 20);
})

  })

view a demo here.

在此处查看演示。

回答by StuperUser

$("textarea").blur(function(event) {
    var maxLength = 3000000;
    var length = this.value.length;
    if (length > maxLength) {
        //reassign substring of max length to text area value
        this.value = this.value.substring(0, maxLength);
        alert(maxLength + ' characters allowed, excess characters trimmed');
    }
});

This jquery attaches the anonymous function to textareas, this will trim the text and alert the user, you can also attach it to the keypress event.

这个 jquery 将匿名函数附加到 textareas,这将修剪文本并提醒用户,您也可以将其附加到按键事件。

See: http://viralpatel.net/blogs/2008/12/set-maxlength-of-textarea-input-using-jquery-javascript.htmlfor further details on that.

有关更多详细信息,请参阅:http: //viralpatel.net/blogs/2008/12/set-maxlength-of-textarea-input-using-jquery-javascript.html

回答by Andres Rojas

You can't access the clipboard content via JS, so you can't prevent it. However, you have three options:

您无法通过 JS 访问剪贴板内容,因此您无法阻止它。但是,您有三个选择:

  1. Don't allow the user to paste content (This would a really bad idea for UX).
  2. Use an onPaste event in order to substr the pasted content from 0 to the limit of characters, once it is pasted.
  3. Use a javascript/flash plugin (This will not work on mobile devices).
  4. Use an applet or SilverLight (I don't like this one)
  1. 不允许用户粘贴内容(这对 UX 来说是一个非常糟糕的主意)。
  2. 粘贴后,使用 onPaste 事件将粘贴的内容从 0 减至字符数限制。
  3. 使用 javascript/flash 插件(这不适用于移动设备)。
  4. 使用小程序或 SilverLight(我不喜欢这个)

These are just some suggestions, and if you find another way to do this, please let me know it.

这些只是一些建议,如果您找到其他方法来做到这一点,请告诉我。

回答by Daniel Vassallo

In IE, you can use the onPasteevent. (MSDN documentation)

在 IE 中,您可以使用该onPaste事件。(MSDN 文档

In Firefox, Safari, Opera and Chrome you can use the onInputevent (Dottoro.com reference). This event fires when the text content of the element is changed through the user interface, including pasting.

在 Firefox、Safari、Opera 和 Chrome 中,您可以使用该onInput事件(Dottoro.com 参考)。当通过用户界面更改元素的文本内容(包括粘贴)时,将触发此事件。

回答by Art

I'd use jQuery and add an event handler for the textarea. When the handler fires, do the count and respond as desired.

我会使用 jQuery 并为 textarea 添加一个事件处理程序。当处理程序触发时,进行计数并根据需要做出响应。