Javascript文本区域限制问题
javascript textarea restriction problem
我有一个文本区域,我为用户输入设置了250个字符的限制。这很好。我添加了一些代码来动态编辑这个框。是否有可能构建"编辑"代码来限制框,所以只有250个字符,就像最初的添加框一样?
添加框:
<<p> jsp/strong>:<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>
javascript :
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 );
}
}
下面是编辑该框....的代码片段我如何在下面构造镜像上面的javascript只是为了阻止输入超过250个字符的框?
function editCommentToggle( id )
{
theRow = document.getElementById("id"+id);
//user = theRow.cells[0].innerHTML;
//date = theRow.cells[1].innerHTML;
com = theRow.cells[2].innerHTML;
idx = 2;
// Comment field
cell = theRow.cells[idx];
while( cell.childNodes.length > 0 ) cell.removeChild(cell.childNodes[0]);
element = document.createElement("textarea");
element.id="comments-"+id;
element.rows="3";
element.value = com;
element.style.width = "400px";
element.maxLength="250";
cell.appendChild(element);
只是补充道:
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 );
}
}
现在我得到:
的值。
...
element.maxLength="250";
//add this
element.onfocus = element.onkeydown = element.onkeyup = function(){return characterCounter(undefined, 250, element);};
//end add
cell.appendChild(element);
...
相关文章:
- 从 tinymce 文本区域检索数据时出现问题
- 使用jquery问题将文本附加到文本区域
- 单击输入区域时出现问题
- 在动态更改的文本区域中输入文本时出现问题
- Lotus Notes web表单-IE中的文本区域问题
- text区域高度请求滚动高度流问题
- 重制版字符文本区域问题
- 同步跨度和文本区域滚动条-滚动条稍微偏离和粘贴问题
- 我的三角形区域构造函数有什么问题
- Knockout JS问题与tinymce文本区域
- j查询验证模糊文本区域与其他输入问题
- 动态注入时保存文本区域值的夏季注释问题
- IE8 换行符问题中的文本区域
- 如何解决离子应用程序内的文本区域选择问题
- 在 javascript 中将文本区域放置在画布上的问题
- Safari 中的 JavaScript 文档文本区域问题
- 获取文本区域值时出现问题
- 在扫雷中清理空白方块周围的区域时遇到问题
- D3 堆栈区域数据格式问题
- 展开/折叠文本区域问题