我正在使用 Igor Vaynberg 的 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">