与Jquery的.on事件绑定
我为我的网站使用了一个自动完成插件。这对我来说很有效。但现在,由于我同时使用Ajax,所以在使用Ajax添加新HTML时,我的脚本失败了。 我在stackoverflow上发布了帖子,得到了关于使用jQuery1.7.2的与Jquery的.on事件绑定,jquery,Jquery,我为我的网站使用了一个自动完成插件。这对我来说很有效。但现在,由于我同时使用Ajax,所以在使用Ajax添加新HTML时,我的脚本失败了。 我在stackoverflow上发布了帖子,得到了关于使用jQuery1.7.2的.on的答案,因此我尝试将我的代码绑定到.on 我的代码是 $(document).ready(function(){ $(function(){ $(document).on("click.autocomplete","#artist_id",func
.on
的答案,因此我尝试将我的代码绑定到.on
我的代码是
$(document).ready(function(){
$(function(){
$(document).on("click.autocomplete","#artist_id",function(e){
$(this).autocomplete({
source : '<?php echo HTTP_PATH . '/artists/getArtistList'; ?>',
multiple: true,
mustMatch: true,
matchContains: true,
scroll: true,
minChars: 0,
autoFill: true,
dataType: "json",
parse: function(data) {
return $.map(data, function(item) {
return { data: item, value: item.label, result: item.label};
});
},
formatItem: function(item) {
return item.label;
},
formatResult: function(item) {
return item.id;
},
formatMatch: function(item) {
return item.label;
}
});
});
});
});
$(文档).ready(函数(){
$(函数(){
$(文档)。在(“单击.自动完成”,“艺术家id”,函数(e){
$(此)。自动完成({
来源:“”,
多重:对,
mustMatch:是的,
对,,
卷轴:没错,
明查斯:0,
自动填充:对,
数据类型:“json”,
解析:函数(数据){
返回$.map(数据、函数(项){
返回{data:item,value:item.label,result:item.label};
});
},
formatItem:函数(项){
退货项目.标签;
},
formatResult:函数(项){
返回item.id;
},
formatMatch:函数(项){
退货项目.标签;
}
});
});
});
});
运行良好的代码
$("#artist_id").focus().autocomplete( '<?php //echo HTTP_PATH . '/artists/getArtistList'; ?>', {
multiple: true,
mustMatch: true,
matchContains: true,
scroll: true,
minChars: 0,
autoFill: true,
dataType: "json",
parse: function(data) {
return $.map(data, function(item) {
return { data: item, value: item.label, result: item.label};
});
},
formatItem: function(item) {
return item.label;
},
formatResult: function(item) {
return item.id;
},
formatMatch: function(item) {
return item.label;
}
});
$(“#艺术家id”).focus().autocomplete(“”{
多重:对,
mustMatch:是的,
对,,
卷轴:没错,
明查斯:0,
自动填充:对,
数据类型:“json”,
解析:函数(数据){
返回$.map(数据、函数(项){
返回{data:item,value:item.label,result:item.label};
});
},
formatItem:函数(项){
退货项目.标签;
},
formatResult:函数(项){
返回item.id;
},
formatMatch:函数(项){
退货项目.标签;
}
});
问题既不是它发送响应,也不是它给我脚本错误。我试图用Firebug检查它。您的问题是这行:
$(document).on("click.autocomplete","#artist_id",function(e){
您以前是“绑定”焦点,现在是“单击.自动完成”。将其更改为:
$(document).on("focus","#artist_id",function(e){
根据,第二个参数是要绑定到的事件,而不是选择器。几件事:
这是多余的
$(document).ready(function(){
$(function(){...
只使用其中一个
$(document).ready(function(){...
其次,click.autocomplete
绑定到任何具有autocomplete
命名空间的click事件。我认为您要做的是将事件绑定到任何具有类自动完成
的文本输入
$(document).on("click",".autocomplete",function(e){
// prevent the default action
e.preventDefault();
// initialize autocomplete
$(this).autocomplete({
...
})
// remove class to prevent reinitializing autocomplete:
.removeClass('autocomplete')
// trigger the focus event to start the autocomplete
.focus();
});
$(文档)。这里不需要ready,因为文档
已经准备好了。您是否尝试过在脚本中放置一个console.log(“此处某物”)以查看您是否已经准备好了?我怀疑这是因为装订不好$(document).on(“click.autocomplete”、“#artist_id”、function(e){try$(document).on(“click”、“#artist_id”、function(e){你能引导放置logconsole.New吗?就在这一行$(this.autocomplete)上方({put console.log(“我做的”);然后在fire bug中,如果你看控制台,如果你看到我做的,你就进入了那个函数。我在控制台中得到了“我做的”,即firebug。你有$(function(){});在$(document)中。准备好了,他们做了同样的事情。它没有显示错误,因为它听起来好像没有正确绑定。@rickm per.on()上的jQuery文档
(您链接到的)第二个参数是可选的选择器,第一个参数始终是事件。@KevinB因为他使用的是$(文档)
他需要选择器。根据这是否是动态元素,您可以省略该参数,只需执行$(“#artist_id”);
我只是指出,根据.on()上的jQuery文档,您在语句中是不正确的,第二个参数是您要绑定到的事件,而不是选择器。
,它是向后的…第一个参数是事件,第二个参数是选择器。