[英]How do I mimic the functionality of this callback in jquery/js?
I am replacing the jQuery-ui Autocomplete library with one that I made. 我用我制作的库替换了jQuery-ui自动完成库。 The old initializer for my input element looks like this:
我的输入元素的旧初始化程序看起来像这样:
$('#search_input').autocomplete({
minLength: 2,
select: function(event, ui) {
$('.some_element').val('ui.item.id');
return false;
}
});
My new auto-complete library looks like this: 我新的自动完成库如下所示:
$.fn.custom_search = function() {
$(this).on('keydown click', (e) function(){
switch e.type {
case 'keydown':
...
search_term = $(this).val();
ajax_caller(search_term);
break;
case 'click'
...
break;
}
};
How can I maintain the 'select' functionality from the jQuery-ui Autocomplete? 如何维护jQuery-ui自动完成功能的“选择”功能? I want everything passed to select in the initializer to fire upon an item being selected from the search results from my custom auto-complete.
我希望通过初始化程序中选择的所有内容,以从自定义自动完成功能的搜索结果中选择一个项目时触发。
The 'ajax_caller' function passes the search term to the controller. 'ajax_caller'函数将搜索项传递给控制器。 The controller compiles a list and returns it to a js.erb which creates a list of search term results and attaches it to the bottom of the input.
控制器将编译一个列表,并将其返回到js.erb,该js.erb创建搜索项结果的列表,并将其附加到输入的底部。
The select callback is supposed to be fired when someone chooses an option from the search term results. 当有人从搜索字词结果中选择一个选项时,应该触发选择回调。
You need to pass the select
callback as an option into your plugin, something like this: 您需要将
select
回调作为选项传递给插件,如下所示:
$.fn.custom_search = function(options) {
options = options || {};
var select = options.select || function() {};
$(this).on('keydown click', (e) function(){
switch e.type {
case 'keydown':
...
search_term = $(this).val();
select($(this), search_term);
...
}
};
And then you will pass it like for jQuery autocomplete: 然后您将像jQuery自动完成一样传递它:
$('#search_input').custom_search({
select: function(element, value) {
$('.some_element').val('ui.item.id');
return false;
}
});
Also check this jQuery plugin template , and you can find more similar templates to help you writing a well-structured plugin. 还要检查此jQuery插件模板 ,您可以找到更多类似的模板来帮助您编写结构良好的插件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.