我使用Igor的Select2 jQuery插件和Infinite Scroll with Remote Data选项为我的网站制作一个自动完成的搜索框。AJAX运行得很好,结果也显示出来了,但它们是不可选的--更改事件从未触发,当您单击结果时,什么也不会发生。
Chrome控制台中也没有出现错误,所以我认为这不是语法错误,而是插件将其误认为是禁用的选择框。编辑:为结果列表尝试了一个单独的单击事件,结果列表也没有被解雇,我现在很确定有什么东西干扰了这些事件。
这是我目前的代码,
// Search
$("#site-search").select2({
placeholder: "Search posts",
minimumInputLength: 3,
ajax: {
url: "http://localhost/mysite/search",
dataType: 'json',
quietMillis: 500,
data: function (term, page) {
return {
q: term,
page_limit: 10,
page: page // page number
};
},
results: function (data, page) {
var more = (page * 10) < data.length; // whether or not there are more results available
// return the value of more to tell if more results can be loaded
return {results: data, more: more};
}
},
formatResult: function(post) {
// return html markup for individual result item
markup = '<img src="'+post.image+'" style="width:40%;float:left;margin-right:5px">';
markup += '<p>'+post.title+'</p>';
markup += '<div class="clearfix"></div>';
return markup;
},
formatSelection: function(post) {
// This shows up in the select box
return post.title;
},
dropdownCssClass: "bigdrop" // apply css that makes the dropdown taller
}).on('change', function(e) {
try {
var slug = e.val.slug;
window.location.href = "http://localhost/mysite/posts/"+slug;
} catch(error) {
console.log('Selected search result is invalid: ');
}
});复选框本身只是一个类型为:隐藏的输入
<input type="hidden" class="bigdrop" id="site-search" style="width:100%;height:auto">发布于 2013-01-10 09:37:48
您的问题似乎是,您的结果数据没有名为"id“的属性。Select2 plugins wants an id field对数据,如果没有,它使选项“不可选”。您可以提供一个id函数来覆盖此行为:
$("#site-search").select2({
id: function(obj) {
return obj.slug; // use slug field for id
},
...https://stackoverflow.com/questions/14193463
复制相似问题