简体   繁体   English

如何在jquery / js中模仿此回调的功能?

[英]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.

相关问题 如何使用* just * jquery / js复制php和jquery / js的组合功能? - How do I replicate the functionality of this combination of php and jquery/js with *just* jquery/js? 如何在Chrome中模仿Greasemonkey / Firefox的unsafeWindow功能? - How can I mimic Greasemonkey/Firefox's unsafeWindow functionality in Chrome? 如何使用提交按钮创建一个文本框,使用js或jQuery单击javascript函数执行回调? - How do I create a text box with a submit button that performs a callback on click to a javascript function using js or jQuery? 如何使用node.js和javascript模仿Facebook的“链接共享”功能 - How to mimic Facebook's “link share” functionality using node.js and javascript 如何在Machina.js中扩展状态的功能? - how do I extend a state's functionality in Machina.js? 用纯js做jquery`success`function的功能? - to do the functionality of jquery `success` function by pure js? 如何在D3.js中禁用缩放功能? - How do I disable my zoom functionality in D3.js? 如何将键盘绑定添加到轮播功能? - jQuery - How do I add keyboard bindings to carousel functionality? - jQuery 如何防止JQuery中的默认选项卡切换功能? - How do I prevent default tab switching functionality in JQuery? 如何在signalR中的我的hubClass中添加回调功能,以在延迟的情况下向客户端更新当前工作状态? - How do i Add callBack Functionality to my hubClass in signalR for update the current work status to client with time delay?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM