Jquery 有助于在 textarea 上强制执行 maxlength 吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15031513/
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 14:15:53  来源:igfitidea点击:

Jquery help to enforce maxlength on textarea?

jqueryvalidationtextarea

提问by Chaka

Here is Jquery to enforce maxlength for textarea when you add the attribute "maxlength". Maxlength is not supported with IE. How do I adjust my code to handle multiple textarea(s) on the page? Currently if I add text to any textarea, they all countdown with the same value.

当您添加属性“maxlength”时,这是 Jquery 强制 textarea 的 maxlength。IE 不支持 Maxlength。如何调整我的代码以处理页面上的多个文本区域?目前,如果我将文本添加到任何文本区域,它们都具有相同的值。

Jquery:

查询:

$(document).ready( function () {

maxLength = $("textarea").attr("maxlength");
    $("textarea").after("<div><span id='remainingLengthTempId'>" 
              + maxLength + "</span> remaining</div>");

    $("textarea").bind("keyup change", function(){checkMaxLength(this.id,  maxLength); } )

});

function checkMaxLength(textareaID, maxLength){

    currentLengthInTextarea = $("#"+textareaID).val().length;
    $(remainingLengthTempId).text(parseInt(maxLength) - parseInt(currentLengthInTextarea));

    if (currentLengthInTextarea > (maxLength)) { 

        // Trim the field current length over the maxlength.
        $("textarea").val($("textarea").val().slice(0, maxLength));
        $(remainingLengthTempId).text(0);

    }
}

Html:

网址:

Skills:
<textarea id="1" maxlength="200" rows="10" cols="60" ></textarea>

Postions:
<textarea id="2" maxlength="200" rows="10" cols="60" ></textarea>

Comments
<textarea id="3" maxlength="100" rows="10" cols="60" ></textarea>

回答by X9DESIGN

This is the best solution! Put this into your script tag in HTML code

这是最好的解决方案!将其放入 HTML 代码中的脚本标记中

$("textarea[maxlength]").on("propertychange input", function() {
    if (this.value.length > this.maxlength) {
        this.value = this.value.substring(0, this.maxlength);
    }  
});

and now use <textarea maxlength="{your limit}"></textarea>for {your limit}chars limit.

现在<textarea maxlength="{your limit}"></textarea>用于{your limit}字符限制。

回答by Chaka

The solution is here:

解决方案在这里:

http://web.enavu.com/daily-tip/maxlength-for-textarea-with-jquery/

http://web.enavu.com/daily-tip/maxlength-for-textarea-with-jquery/

$(document).ready(function() {

    $('textarea[maxlength]').keyup(function(){
        //get the limit from maxlength attribute
        var limit = parseInt($(this).attr('maxlength'));
        //get the current text inside the textarea
        var text = $(this).val();
        //count the number of characters in the text
        var chars = text.length;

        //check if there are more characters then allowed
        if(chars > limit){
            //and if there are use substr to get the text before the limit
            var new_text = text.substr(0, limit);

            //and change the current text with the new text
            $(this).val(new_text);
        }
    });

});

回答by Harry Sarshogh

Cant you use this :

你不能用这个:

    class="validate[required,maxSize[50]] " 

回答by Anju313

This solution is working as native HTML text-area , MAX-Length property

此解决方案作为本机 HTML text-area , MAX-Length 属性工作

Var MaxLength=100; 
$('#Selector').keydown(function (e) {
            var text = $(this).val();
            var chars = text.length;
            if (chars > MaxLength) {
                if (e.keyCode == 46 || e.keyCode == 8 || e.keyCode == 37 || e.keyCode == 38 || e.keyCode == 39 || e.keyCode == 40) {
                    return true;
                }
                return false;
            }
            return true;
        });

Don't set the Maxlength property in the server side control (asp:TextBox) as server won't convert maxLength property to HTML markup when returning to your browser.

不要在服务器端控件 (asp:TextBox) 中设置 Maxlength 属性,因为服务器在返回浏览器时不会将 maxLength 属性转换为 HTML 标记。