我正在尝试编写一个函数,以便当textarea填充80个字符时,键盘将被禁用,直到用户按Enter键开始新行,然后他们可以再次键入。
var len = 0;
$('#texttype').on("keypress", function() {
len++;
});
$('#texttype').keyup(function(event) {
var max = 80;
if (len >= max) {
$('#charNum').text('You have reached the limit.');
$("#texttype").keydown(function(event) {
return false;
});
}
});
上面的代码可以在达到限制时禁用键盘,但我无法重新启用访问权限。谢谢!
答案 0 :(得分:0)
试试这个:
var len = 0;
var max = 80;
$('#texttype').on("keypress", function (event) {
len++;
if (len >= max) {
$('#charNum').text('You have reached the limit.');
if (event.keyCode != 13){ return false;}
else{ len = 0;}
});
});
答案 1 :(得分:0)
var len = 0;
var max = 80;
$('#texttype').on("keypress",function(){
len++;
});
$('#texttype').keyup(function (event) {
if (len >= max) {
$('#charNum').text('You have reached the limit.');
$("#texttype").keydown(function(event) {
if (event.keyCode === 13) {
len = 0;
}
else {
event.preventDefault();
}
});
});