最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery UI Autocomplete Suggestions Not Closing on Select - Stack Overflow

programmeradmin0浏览0评论

I have an odd problem with a jQuery UI autoplete. I want it so that when the text field gets focus, a list of all options in the autoplete es up. This works, but when selecting an item by clicking on it, the suggestions stay open. I want the suggestions list to disappear like autoplete normally works.

It works fine when you select the item with your keyboard, but when you click an item, the list just keeps reappearing again over and over again.

To make this problem weirder, the functionality works perfectly when just passing values manually. It only starts happening when I'm passing in a JSON source.

Any ideas!?

Working Code - ,js,output

$(document).ready(function(){

   var test = [ { value: "1",label: "Google" }, { value: "2", label:"StackOverflow" }, { value: "3", label:"Microsoft" }, { value: "4", label:"Yahoo" } ];

   $("input").autoplete({
     source: test,
     delay: 0,
     minLength: 0,
     select: function(event, ui) {      
        event.preventDefault();
        $(this).val(ui.item.label).attr('title', ui.item.label);
     }
  }).focus(function () {
      $(this).val('').autoplete("search");
  });

});

Broken Code - ,js,output

$(document).ready(function(){

   $("input").autoplete({
      source: '',
      delay: 0,
      minLength: 0,
      select: function(event, ui) {      
         event.preventDefault();
         $(this).val(ui.item.label).attr('title', ui.item.label);   
      }
   }).focus(function () {
      $(this).val('').autoplete("search");
   });

});

I have an odd problem with a jQuery UI autoplete. I want it so that when the text field gets focus, a list of all options in the autoplete es up. This works, but when selecting an item by clicking on it, the suggestions stay open. I want the suggestions list to disappear like autoplete normally works.

It works fine when you select the item with your keyboard, but when you click an item, the list just keeps reappearing again over and over again.

To make this problem weirder, the functionality works perfectly when just passing values manually. It only starts happening when I'm passing in a JSON source.

Any ideas!?

Working Code - http://jsbin./aFeceTe/1/edit?html,js,output

$(document).ready(function(){

   var test = [ { value: "1",label: "Google" }, { value: "2", label:"StackOverflow" }, { value: "3", label:"Microsoft" }, { value: "4", label:"Yahoo" } ];

   $("input").autoplete({
     source: test,
     delay: 0,
     minLength: 0,
     select: function(event, ui) {      
        event.preventDefault();
        $(this).val(ui.item.label).attr('title', ui.item.label);
     }
  }).focus(function () {
      $(this).val('').autoplete("search");
  });

});

Broken Code - http://jsbin./uyOGUVU/6/edit?html,js,output

$(document).ready(function(){

   $("input").autoplete({
      source: 'http://jsbin./IdIXIRU/3/js',
      delay: 0,
      minLength: 0,
      select: function(event, ui) {      
         event.preventDefault();
         $(this).val(ui.item.label).attr('title', ui.item.label);   
      }
   }).focus(function () {
      $(this).val('').autoplete("search");
   });

});
Share Improve this question asked Oct 4, 2013 at 12:03 BT643BT643 3,8555 gold badges38 silver badges56 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

Try this

 $(document).ready(function(){
    var temp = true;
    $("input").autoplete({
        source: 'http://jsbin./IdIXIRU/3/js',
        delay: 0,
    minLength: 0,
        select: function(event, ui) {
            event.preventDefault();
            $(this).val(ui.item.label).attr('title', ui.item.label);
            temp = true;
            return false;            
        }
  }).focus(function () {
       if(temp) {
         $(this).autoplete("search");
         temp = false;
     }
   });
});

SEE DEMO

发布评论

评论列表(0)

  1. 暂无评论