一旦达到给定数量的单词或字符(不包括空格),我试图限制表单字段中文本框的长度。
我遇到的麻烦是,一旦达到上限,我就无法弄清楚如何限制子串最大长度变量。
我使用Callable.js(https://sacha.me/Countable/docs/)作为计数器脚本。
var wordL = 3;
var charL = 18;
var boxid = 'descr';
var area = document.getElementById(boxid);
function fixlength(boxid,wordlimit,charlimit,wordcount,charcount,allcount) {
if (charcount > charlimit || wordcount > wordlimit) {
/******************************
* The following line contains the problematic dynamic variable
*******************************/
area.value = area.value.substring(0, all);
}
}
function callback (counter) {
console.log(counter);
document.getElementById("wordsleft").innerHTML = wordL - counter.words;
document.getElementById("charsleft").innerHTML = charL - counter.characters;
fixlength(boxid,wordL,charL,counter.words,counter.characters,counter.all);
}
Countable.live(area, callback);

<script src="https://raw.githubusercontent.com/RadLikeWhoa/Countable/master/Countable.js"></script>
<!DOCTYPE HTML>
<html>
<head>
<title>Test</title>
</head>
<body>
Words:<span id="wordsleft"></span> Chars:<span id="charsleft"></span>
<form>
<textarea id="descr" name"descr"></textarea>
</form>
</body>
</html>
&#13;