jQuery生活问题 - undefined不是函数
问题描述:
我有一个实时搜索ajax和即时获取控制台日志错误Uncaught Typeerror: undefined is not a function.
该错误指向我的代码的第一行。我以前用过这个,它不知道现在发生了什么,有什么想法?jQuery生活问题 - undefined不是函数
$("input#search-staff").live("keyup", function() {
// Set Search String
var staffname = $(this).val();
// Do Search
if(staffname !== ''){
$.ajax({
type: "GET",
url: "_ajax/_tasks/search-staff.php",
data: { query: staffname },
cache: false,
success: function(html){
$("ul#staff-list").html(html);
}
});
}return false;
});
答
这里就是你的函数可以样子:
$(document).on("keyup", "input#search-staff", function(e) {
var staffname = $(this).val();
e.preventDefault();
if (!staffname) return;
$.get("_ajax/_tasks/search-staff.php", {query: staffname})
.done(function (html) {
$("ul#staff-list").html(html);
});
});
注
- 开关从
.live()
到.on()
事件代表团 - 回报早 - 较少深入嵌套代码
- 开关从
.ajax()
至.get()
w第i个jqXhr延迟事件处理(.done()
)
P.S:尽量不要做这样的事情cache: false
在客户端上。只需在响应头文件(Cache-Control: no-cache, no-store
)中设置适当的HTTP缓存指令并将客户端保留为默认值即可。
您确定DOM中存在'$(“input#search-staff”)元素吗? – eithed 2014-09-01 16:53:51
[.live](http://api.jquery.com/live/)已弃用。考虑使用[事件委托](http://learn.jquery.com/events/event-delegation/)而不是 – Stryner 2014-09-01 16:54:49
它在dom中存在 – user934902 2014-09-01 16:55:23