javascript - 未捕获类型错误无法读取未定义的属性 'length'

标签 javascript jquery typeerror

我无法找到问题的解决方案(或者我只是错过了),所以我尝试从您那里得到答案。

我有以下 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/

相关文章:

javascript - 只需要刷新div而不是整个页面

jquery - 如何捕获 Controller 操作的返回值

javascript - jquery更新后div内容被清除

Python 类型错误 : an integer is required weird

javascript - 未捕获的类型错误 : Can not set propery 'FUNCTION_NAME' of undefined for widget implementation

python - 无法将元组用作字典键?

DOM 元素上的 Javascript 转换

javascript - JS>将HTML onclick事件绑定(bind)到自定义元素v1对象

javascript - 如何在javascript中定义速记函数

jquery - 菜单导航不会使用 Jquery addClass() 更改样式