Microsoft JScript 运行时错误:无法获取属性“value”的值:对象为 null 或未定义

Microsoft JScript runtime error: Unable to get value of the property 'value': object is null or undefined

本文关键字:对象 的值 null 未定义 value 运行时错误 JScript 属性 获取 Microsoft      更新时间:2023-09-26

我正在开发一个 asp.net 网页,有一个texbox,我想确保输入的文本少于3个字符,我写的代码是

<input type="text" name="searchcatid" size="15" onblur="test_length(Index.searchchatid)" />
<script>
function test_length(testcontrol) {

    var teststring = testcontrol.value;
    if (teststring.length >= 3) {
        alert("ID must be 3 or fewer characters!");
        testcontrol.focus();
    }
    else {
    }

} 

</script>

我收到错误"Microsoft JScript 运行时错误:无法获取属性'value'的值:对象为 null 或未定义",每当我输入某些内容时,我都不知道出了什么问题,因为我在 asp.net 中真的很新,甚至不确定我的代码是否真的有效,希望有人可以帮助我,非常感谢

以正确的方式传递输入元素:

<input type="text" name="searchcatid" size="15" onblur="test_length(this);" />

如果您通过 JavaScript 注册事件处理程序而不是使用内联事件,那就更好了。

是问题吗,请检查

name="searchcatid" Index.searchchatid拼写searchcatid不匹配