这是准确报告console.log中文本字段的长度,但它也会在每个keyup上的代码下面抛出错误。有什么想法吗?JavaScript长度函数在文本字段上抛出错误
$('.search').keyup(function() {
var searchTerm = this.value;
console.log(this.value.length);
if (this.value.length>0){
getSuggestions(searchTerm);
}else{
$('#Button').click();
}
})
而这就是我得到了Chrome的控制台
Uncaught TypeError: Cannot read property 'length' of undefined pm_functions.js:283
(anonymous function) pm_functions.js:283
p.event.dispatch jquery.min.js:2
g.handle.h
什么是元素(S)与类“搜索”的HTML我解决?你能够确定(在Chrome脚本调试器视图中)确定这是导致问题的代码行吗?如果'console.log()'起作用,后续行会引发异常似乎很奇怪。 – Pointy 2013-04-21 14:42:23
你可能的意思是'$(this)'不'这个' – 2013-04-21 14:44:41
尝试$(this).val()和/或$(this).text()而不是this.value – 2013-04-21 14:45:12