gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 11:56:04 25 4
gpt4 key购买 nike

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

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com