0

大家好,我在这里有这个脚本,它计算字符和行数,现在虽然它仍然允许行和字符进入减号,但我希望它也可以设置一个警告框,这样它就可以阻止用户能够多加点文字,你能帮帮我吗?

javascript:

function textCounter(theField,theCharCounter,theLineCounter,maxChars,maxLines,maxPerLine) {
    var strTemp = "";
    var strLineCounter = 0;
    var strCharCounter = 0;

    for (var i = 0; i < theField.value.length; i++)
    {
        var strChar = theField.value.substring(i, i + 1);
        if (strChar == '\n')
        {
            strTemp += strChar;
            strCharCounter = 1;
            strLineCounter += 1;
        }
        else if (strCharCounter == maxPerLine)
        {
            strTemp += '\n' + strChar;
            strCharCounter = 1;
            strLineCounter += 1;
        }
        else
        {
            strTemp += strChar;
            strCharCounter ++;
        }
    }

    theCharCounter.value = maxChars - strTemp.length;
    theLineCounter.value = maxLines - strLineCounter;
}

并在代码中使用:

<textarea name="comment" cols="50" rows="10" wrap="VIRTUAL" onKeyUp="textCounter(theForm.comment,theForm.remChars,remLines,900,30,50);"></textarea>
<br><input name=remChars type=text value="900" size=3 maxlength=3 readonly> characters left
<br><input name=remLines type=text value="30" size=3 maxlength=3 readonly> lines left<br>

向主持人报告

4

1 回答 1

1

1 - 在函数末尾添加一个检查:

var check = strTemp.length <= maxChars && strLineCounter <= maxLines;
if (!check) alert("Error message here!");

2 - 修剪字段的文本,使其仅包含所需的字符:

theField.value = theField.value.sbustring(0, maxChars - 1);

3 - 在函数末尾添加一个返回:

return check ;

4 - 并将事件绑定更改为:

onKeyUp="return textCounter(theForm.comment,theForm...
于 2012-04-08T21:38:14.547 回答