我无法找到问题的解决方案(或者我只是错过了),所以我尝试从您那里得到答案。
我有以下 html:
<input name="title" type="text" id="title" size="69" maxlength="30" onmouseout="CheckFieldLength(title, 'charcount2', 'remaining2', 30);" onkeydown="CheckFieldLength(title, 'charcount2', 'remaining2', 30);" onkeyup="CheckFieldLength(title, 'charcount2', 'remaining2', 30);"/></td>
我试图获取输入字段的字符长度,如下所示:
<span id="charcount2">0</span> characters entered | <span id="remaining2">140</span> characters remaining
js 文件如下所示:
function CheckFieldLength(fn,wn,rn,mc) {
var len = fn.value.length;
if (len > mc) {
fn.value = fn.value.substring(0,mc);
len = mc;
}
document.getElementById(wn).innerHTML = len;
document.getElementById(rn).innerHTML = mc - len;
}
但是,我不断收到以下错误:
uncaught typeerror cannot read property 'length' of undefined
你知道如何解决这个问题吗?
最佳答案
像这样使用该函数
CheckFieldLength(this, 'charcount2', 'remaining2', 30);
关于javascript - 未捕获类型错误无法读取未定义的属性 'length',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25613886/