2013-10-04 51 views
1

我有一個jQuery UI自動完成的奇怪問題。我希望它能夠在文本字段獲得焦點時自動完成所有選項列表。這是有效的,但當選擇時點擊就可以了,建議保持打開狀態。我想建議列表消失像自動完成正常工作。jQuery UI自動完成建議不關閉選擇

當您使用鍵盤選擇項目時,它可以正常工作,但是當您點擊某個項目時,該列表會一直重複出現。

爲了解決這個問題weirder,當手動傳遞值時,該功能可以完美工作。它只在我傳入JSON源時纔會發生。

任何想法!?

工作守則 - http://jsbin.com/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").autocomplete({ 
    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('').autocomplete("search"); 
    }); 

}); 

斷碼 - http://jsbin.com/uyOGUVU/6/edit?html,js,output

$(document).ready(function(){ 

    $("input").autocomplete({ 
     source: 'http://jsbin.com/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('').autocomplete("search"); 
    }); 

}); 

回答

1

試試這個

$(document).ready(function(){ 
    var temp = true; 
    $("input").autocomplete({ 
     source: 'http://jsbin.com/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).autocomplete("search"); 
     temp = false; 
    } 
    }); 
}); 

SEE DEMO

+0

嗯,這是如此接近!唯一的問題是,如果您雙擊文本字段,然後單擊開箱即可,然後再次單擊,自動完成功能不再有效:( – BT643

+0

Phew!Legend!我剛剛向自動完成添加了一個關閉事件以重置temp變量,我已經將它添加到你的代碼中了 – BT643