這是準確報告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