简体   繁体   English

对从jQuery ui自动完成选项列表中选择的选项执行功能

[英]Execute function on option selected from jQuery ui autocomplete option list

Hi I have the code below for autocomplete which works fine but my only issue is that when a user clicks on an option from the autocomplete list, it should trigger another function (doSomething()). 嗨,我有下面的自动完成代码可以正常工作,但是我唯一的问题是,当用户单击自动完成列表中的选项时,它将触发另一个函数(doSomething())。 This however is not being done. 但是,这尚未完成。 Granted if the user makes a selection and presses "enter" the function is executed. 如果用户进行选择并按下“输入”,则将执行该功能。

var url = "http://myURL";
var field = "myField";

$(document).ready(function () {
  $("#tags").autocomplete({  
      source: function (req, add) {
          var suggestions = search(req.term, url, field);
          add(suggestions);  
      },
       select: function( event, ui ) {
        doSomething();          
  }
  });

 });  


function search(value, listurl, field) {
  var coll = new Array();
  var url =  
      listurl + "?$filter=startswith(" + field + ",'" + value + "')";

  $.ajax({
      cache: true,
      type: "GET",
      async: false,
      dataType: "json",
      url: url,  
      success: function (data) {
          var results = data.d.results;
          for (att in results) {
              var object = results[att];
              for (attt in object) {
                  if (attt == field) {
                      coll.push(object[attt]);
                  }
              }
          }
      }

  });
  return coll;}

function doSomething() {
}

Thanks for any suggestions. 感谢您的任何建议。

Got this resolved like this: 得到这样解决:

$('#tags').on('keyup change', function () {
        doSomething();
    }).change();

$('#tags').on('autocompleteselect', function (e, ui) {
    doSomething();
    });

Thanks to this SO link 感谢这个SO链接

I know this is an old topic, but I came across this problem and I found another solution which I believe JQuery provides for. 我知道这是一个老话题,但是遇到了这个问题,我找到了JQuery提供的另一种解决方案。 You can use the close event to do this. 您可以使用close事件来执行此操作。

Examples (extracted from http://api.jqueryui.com/autocomplete/ and adapted for this question): 示例(摘自http://api.jqueryui.com/autocomplete/,并针对此问题进行了修改):

1) When you initialize the autocomplete 1)初始化自动完成时

$( "#tags" ).autocomplete({
  close: function( event, ui ) { doSomething(); }
});

or 要么

2) Binding an listener to the close event 2)将侦听器绑定到close事件

$( "#tags" ).on( "autocompleteclose", function( event, ui ) { doSomething(); } );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM