html文本区域的javascript错误
我刚刚在我的页面中添加了一个文本区域,它是另一个页面的“包含”…我有一些脚本在文本区域中执行计数器…下面是代码:html文本区域的javascript错误,javascript,html,jsp,Javascript,Html,Jsp,我刚刚在我的页面中添加了一个文本区域,它是另一个页面的“包含”…我有一些脚本在文本区域中执行计数器…下面是代码: function characterCounter(spanId, maxLen, inputElement) { if (spanId) { // Update the counter the user sees. var whatIsLeft = maxLen - inputElement.value.length; if ( wha
function characterCounter(spanId, maxLen, inputElement)
{
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 );
}
}
替换:
onkeydown="characterCounter(commentsCounter,250, this)"
onkeyup="characterCounter(commentsCounter,250, this)">
与
及
与
目前的代码期望CommentsCenter是一个全局可访问的变量。为了访问id为CommentsCenter的元素,您必须使用文档。getElementById您的函数似乎期望为
spanID
提供一个DOM节点,并且您正在传递CommentsCenter
的id而不带引号。在加载
后,您可能希望将其放在
标记中的某个位置:
You have <strong><span id="commentsCounter">${const['COMMENT_MAX_LENGTH'] - fn:length(commentForm.comment)}</span></strong> characters left.<br />
<script type='text/javascript'>
var commentsCounter = document.getElementById("commentsCounter");
</script>
您还有${const['COMMENT\u MAX\u LENGTH']-fn:LENGTH(commentForm.COMMENT)}个字符。
var commentsconter=document.getElementById(“commentsconter”);
然后,您不需要修改textarea的关键事件处理程序,因为CommentsCenter
现在是一个正确的非空变量。使用以下内容:
<textarea id="comment" name="comment" rows="2" cols="125"
onkeypress="characterCounter(\"commentsCounter\",${const['COMMENT_MAX_LENGTH']}, this)"
onkeydown="characterCounter(\"commentsCounter\",${const['COMMENT_MAX_LENGTH']}, this)"
onkeyup="characterCounter(\"commentsCounter\",${const['COMMENT_MAX_LENGTH']}, this)">
</textarea>
必须始终引用假定为字符串类型的参数。如果引号内有引号,那么在java脚本中,您可以使用“(双引号)介于“(单引号)之间,或者反之亦然,如果需要,可以选择使用转义字符,就像我在上面的示例中使用的那样。您只需将\“替换为”
谢谢
Shailendra唯一的一件事…javascript函数被许多不同的东西使用…所以我不能更改该函数,然后只需将其更改为
characterCounter(document.getElementById('CommentsCenter'),250,这个)
。我添加了以下内容:''我认为问题在于访问位于其on页面中的javascript页面。当我将脚本放在jsp的顶部并添加单引号时。这是唯一有效的一件事…javascript函数被许多不同的东西使用…所以我不能更改该函数我的示例不要求您更改该函数。您只需在
之后添加
标记,您的函数将继续正常工作。您应该只将函数绑定到其中一个事件。。。
onkeydown="characterCounter('commentsCounter',250, this)"
onkeyup="characterCounter('commentsCounter',250, this)">
function characterCounter(spanId, maxLen, inputElement)
{
if (spanId)
function characterCounter(id, maxLen, inputElement)
{
spanId = document.getElementById(id);
if (spanId)
You have <strong><span id="commentsCounter">${const['COMMENT_MAX_LENGTH'] - fn:length(commentForm.comment)}</span></strong> characters left.<br />
<script type='text/javascript'>
var commentsCounter = document.getElementById("commentsCounter");
</script>
<textarea id="comment" name="comment" rows="2" cols="125"
onkeypress="characterCounter(\"commentsCounter\",${const['COMMENT_MAX_LENGTH']}, this)"
onkeydown="characterCounter(\"commentsCounter\",${const['COMMENT_MAX_LENGTH']}, this)"
onkeyup="characterCounter(\"commentsCounter\",${const['COMMENT_MAX_LENGTH']}, this)">
</textarea>