Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/424.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 - Fatal编程技术网

javascript文本区域限制问题

javascript文本区域限制问题,javascript,Javascript,我有一个文本区域,我给用户输入设置了250个字符的限制。这个很好用。我添加了一些代码来动态编辑该框。是否可以构造“编辑”代码以限制框,使其仅包含250个字符,与初始添加框的方式相同 下面是用于添加的框: jsp: <td colspan="3"> You have <strong><span id="commentsCounter">${const['COMMENT_MAX_LENGTH']}</span></strong> c

我有一个文本区域,我给用户输入设置了250个字符的限制。这个很好用。我添加了一些代码来动态编辑该框。是否可以构造“编辑”代码以限制框,使其仅包含250个字符,与初始添加框的方式相同

下面是用于添加的框:

jsp

<td colspan="3">
    You have <strong><span id="commentsCounter">${const['COMMENT_MAX_LENGTH']}</span></strong> characters left.<br/>
    <textarea id="comment" name="comment" rows="3"
        onfocus="characterCounter('commentsCounter',${const['COMMENT_MAX_LENGTH']}, this)"
        onkeydown="characterCounter('commentsCounter',${const['COMMENT_MAX_LENGTH']}, this)"
        onkeyup="characterCounter('commentsCounter',${const['COMMENT_MAX_LENGTH']}, this)"></textarea>
        <a href="javascript:addComment();"><img src="../images/icon_add.gif" border="0" alt="Add"/></a>
</td>
    function characterCounter(id, maxLen, inputElement)
{
    spanId = document.getElementById(id);

    if (spanId)
    {
        // Update the counter the user sees.
        var whatIsLeft = maxLen - inputElement.value.length;

        if ( whatIsLeft < 0 ) whatIsLeft = 0;
        spanId.innerText = whatIsLeft;
    }

    // Restrict user from entering more than the maxlen.
    if ( inputElement.value.length > maxLen )
    {
        inputElement.value = inputElement.value.substring( 0, maxLen );
    }
}
刚刚添加:

maxlength = 250;
element.onfocus = element.onkeydown = element.onkeyup = function(){return characterCounterEdit(undefined, maxlength, element);}; 
        cell.appendChild(element);

 function characterCounterEdit(id, maxLen, inputElement)
{
    spanId = document.getElementById(id);

    if (spanId)
    {
        // Update the counter the user sees.
        var whatIsLeft = maxLen - inputElement.value.length;

        if ( whatIsLeft < 0 ) whatIsLeft = 0;
        spanId.innerText = whatIsLeft;
    }

    // Restrict user from entering more than the maxlen.
    if ( inputElement.value.length > maxLen )
    {
        inputElement.value = inputElement.value.substring( 0, maxLen );
    }
}
maxlength=250;
element.onfocus=element.onkeydown=element.onkeyup=function(){return characterCounterEdit(未定义,maxlength,element);};
子单元(元素);
函数字符计数器编辑(id、maxLen、inputElement)
{
spanId=document.getElementById(id);
如果(spanId)
{
//更新用户看到的计数器。
var whatisleet=maxLen-inputElement.value.length;
如果(whatisleet<0)whatisleet=0;
spanId.innerText=whatisleet;
}
//限制用户输入超过最大值的内容。
if(inputElement.value.length>maxLen)
{
inputElement.value=inputElement.value.substring(0,maxLen);
}
}
现在我得到:


“value.length”为空或不是对象

Hey thanx man,但是${const['COMMENT\u MAX\u length']}在此页面上不可识别……我应该使用element.maxLength=250吗?只需使用普通250即可。它编辑了上面的代码以反映这一点。
...
element.maxLength="250";
//add this
element.onfocus = element.onkeydown = element.onkeyup = function(){return characterCounter(undefined, 250, element);};
//end add
cell.appendChild(element);
...
...
element.maxLength="250";
//add this
element.onfocus = element.onkeydown = element.onkeyup = function(){return characterCounter(undefined, 250, element);};
//end add
cell.appendChild(element);
...