2011-10-19 78 views
0

我有一些autocompletes終於工作,但有一種行爲,我想改變。如果用戶鍵入部分值並單擊返回列表之外的任何位置,則會在輸入中設置部分值。如果用戶沒有選擇一個返回的選項,我寧願該值保持空白。jquery自動完成行爲修改

它看起來像的焦點事件是什麼,我應該改變,但我發現了API彆扭......它說:

Before focus is moved to an item (not selecting), ui.item refers to the focused 
item. The default action of focus is to replace the text field's value with the 
value of the focused item, though only if the focus event was triggered by a 
keyboard interaction. Canceling this event prevents the value from being updated, 
but does not prevent the menu item from being focused. 

,但它並沒有給出如何取消事件爲例。

回答

1

找到答案的另一個問題:)修改的變化事件,像這樣似乎這樣的伎倆:

$('#myID').autocomplete({ 
      source: function(request, response) { 
       $.ajax({ 
        url: "cfc/autoSuggest.cfc?method=someMethod&returnformat=json", 
        dataType: "json", 
        data: { 
         search: request.term, 
         maxRows: 20 
        }, 
        success: function(data) { 
         response(data); 
        }     
       }) 
      }, 
      change: function(event, ui) {  
       if (!ui.item) { 
        $(this).val(''); 
       } 
      } 
     });