Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将所有文本区域限制为指定的字符数_Javascript_Jquery_Textarea - Fatal编程技术网

Javascript 将所有文本区域限制为指定的字符数

Javascript 将所有文本区域限制为指定的字符数,javascript,jquery,textarea,Javascript,Jquery,Textarea,页面上有许多文本区域。数字是动态的,不是固定的。我需要限制一页上所有文本区域的长度。在js或jquery中如何实现这一点 我的尝试:- <body> <div id="contact"> <form action="" method="post"> <fieldset> <table style="width: 100%;">

页面上有许多文本区域。数字是动态的,不是固定的。我需要限制一页上所有文本区域的长度。在js或jquery中如何实现这一点


我的尝试:-

<body>
        <div id="contact">
            <form action="" method="post">
                <fieldset>
                    <table style="width: 100%;">
                        <tr class="questionsView" style="width: 100%;margin: 5px;">
                            <td class="mandatory">
                                <b>1&nbsp;
                                    *Qywerew we</b>
                                <hr/>
                                <table class="profileTable" style="margin-left: 25px;">
                                    <tr>
                                        <td>
                                            <textarea style="border: solid 1px #800000;" rows="5" name="165" cols="100">
                                            </textarea>
                                        </td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                &nbsp;
                            </td>
                        </tr>
                        <tr class="questionsView" style="width: 100%;margin: 5px;">
                            <td class="">
                                <b>2&nbsp;
                                    a da da da</b>
                                <hr/>
                                <table class="profileTable" style="margin-left: 25px;">
                                    <tr>
                                        <td>
                                            <textarea style="border: solid 1px #800000;" rows="5" name="166" cols="100">
                                            </textarea>
                                        </td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                &nbsp;
                            </td>
                        </tr>
                    </table>
                    <input type="submit" value="Submit" onclick="return checkThis()">
                </fieldset>
            </form>
        </div>
        <script>
            $('textarea').bind('paste keyup blur', function(){
                $(this).val(function(i, val){
                    return val.substr(0, 5);
                });
            });
        </script>
    </body>

1.
*我们是谁

2. 阿达达达
$('textarea').bind('paste keyup blur',function(){ $(this.val)(函数(i,val){ 返回值substr(0,5); }); });

更新 我不知道为什么,但它每次都在文本区域打印
函数(I,val){return val.substr(0,5);}

听起来您使用的是较旧版本的jQuery(前
1.4
)。下面的重构代码将起作用

$('textarea').bind('paste keyup blur', function() {
    $(this).val(this.value.substr(0, 5));
});


前面的代码在jQuery 1.4之前不起作用,因为它只需要一个字符串作为
val()
的参数。通过传递一个函数,它的
toString()
被隐式调用,返回函数的字符串表示形式。

可怕而激进的方式

var maxLettersCount = 3;

$('textarea').bind('keydown paste', function(event) {
    var textarea = $(event.target),
        text = textarea.val(),
        exceptionList = {
            8: true,
            37: true,
            38: true,
            39: true,
            40: true,
            46: true
        };

    if (event.type == 'keydown' && text.length >= maxLettersCount && !exceptionList[event.keyCode]) {
        return false;
    } else if (event.type == 'paste') {
        setTimeout(function() {
            textarea.val(textarea.val().substring(0, maxLettersCount));
        }, 1);
    }
})
setInterval(function(){
    $('textarea').each(function(){
        if (this.value.length > 5){
            this.value = this.value.substr(0,5);
        }
    })
}, 1)

我最近也有同样的任务,在所有浏览器中都找不到可靠的东西。具体来说,我在使用右键单击上下文菜单粘贴文本时遇到了问题。我可能会将其复制为
.bind('paste keyup blur',…)
-我过去使用过与您类似的代码,但我发现对
粘贴
事件的跨浏览器支持有点粗略,拖放不会触发粘贴或keyup。添加
blur
会捕获其他事件获得的所有信息,即使有点晚。这对我也不起作用。具体来说,我可以使用右键单击上下文菜单(Firefox 6)将长测试粘贴到文本区域。@Kobi对于
paste
事件的工作来说太多了。我也是这样。我将进行编辑。@mark-如果它根本不起作用(与Kobi和我指出的特殊情况相比),请确认您在页面上放置代码的位置-应该在document ready函数中(或者至少在textareas之后的底部)。@alex:我不知道为什么,但它会打印
函数(I,val){return val.substr(0,5)}
每次都在文本区域中。在
事件中,您可以添加事件键代码,如果用户粘贴(通过键盘、上下文菜单或主菜单),则哪些块不起作用?拖放怎么样?“是”和“否”常量有什么用?
true
false
太时髦了?解决方案非常有用-intensive@ant_Ti当然,如果不是的话,我不会说这很可怕。
setInterval(function(){
    $('textarea').each(function(){
        if (this.value.length > 5){
            this.value = this.value.substr(0,5);
        }
    })
}, 1)