Javascript 未捕获的类型错误:无法读取 null 的属性“长度”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8107235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Uncaught TypeError: Cannot read property 'length' of null
提问by i-CONICA
I'm having a problem with an ajax script that checks postcodes against a region table in the database to find which area the postcode relates to, then updates the region select list with this information.
我在使用 ajax 脚本时遇到问题,该脚本根据数据库中的区域表检查邮政编码以查找邮政编码与哪个区域相关,然后使用此信息更新区域选择列表。
There's nothing wrong with the ajax scripting at all, because it works from another page just fine. A particular page (in an admin area I can't give access to) is having problems and my debugger says "Uncaught TypeError: Cannot read property 'length' of null" when the call to the remote script is triggered.
ajax 脚本完全没有问题,因为它在另一个页面上工作得很好。特定页面(在我无法访问的管理区域中)出现问题,并且我的调试器在触发对远程脚本的调用时显示“未捕获的类型错误:无法读取 null 的属性‘长度’”。
I'm baffled by it, because I'm clueless as to what it's suggesting is null. I understand what it's saying, that it can't read the length of null, obviously, but I can't see what it could be that's null. It may be being given an empty string, but an empty string doesn't trigger this error on the other pages, and an empty string isn't null.
我对此感到困惑,因为我对它所暗示的内容是空的一无所知。我明白它在说什么,显然它无法读取 null 的长度,但我看不出它可能是 null。它可能被赋予一个空字符串,但空字符串不会在其他页面上触发此错误,并且空字符串不为空。
I know I've not given much to go on, but if you could offer any more debugging advice, it'd be great.
我知道我没有提供太多的东西,但如果你能提供更多的调试建议,那就太好了。
Thanks.
谢谢。
回答by i-CONICA
Peter Olson's suggestion was great, I've since explored and discovered lots of powerful debugging tools built into Chrome (I'm sure Firebug, etc, have the same stuff).
Peter Olson 的建议很棒,此后我探索并发现了许多内置于 Chrome 中的强大调试工具(我确信 Firebug 等也有相同的东西)。
It turned out to be that a script used getElementById on an element that didn't have an Id. Hence why the script worked fine on the other pages (the inputs on the other pages had IDs)
结果是脚本在没有 Id 的元素上使用了 getElementById。因此,为什么脚本在其他页面上运行良好(其他页面上的输入有 ID)
I've fixed the problem now.
我现在已经解决了这个问题。
Thanks a lot.
非常感谢。