2013-03-24 95 views
1

我在Wordpress插件中使用jQuery自動完成。無法聚焦時,如何取消ajax/autocomplete? $first_id由PHP傳遞並允許多個實例。jquery自動完成取消未聚焦

jQuery(document).ready(function ($){ 

    $("#'.$first_id.'").autocomplete({ 
    source: function(req, add){ 
     $.getJSON("'.site_url().'/wp-admin/admin.php?page=church_admin/index.php&action=get_people&callback=?", req, function(data) { 

     //create array for response objects 
     var suggestions = []; 

     //process response 
     $.each(data, function(i, val){ 
      suggestions.push(val.name); 
     }); 

     //pass array to callback 
     add(suggestions); 
     }); 

    }, 
    select: function (event, ui) { 
     var terms = $("#'.$first_id.'").val().split(", "); 
     // remove the current input 
     terms.pop(); 
     console.log(terms); 
     // add the selected item 
     terms.push(ui.item.value); 
     console.log(terms); 
     // add placeholder to get the comma-and-space at the end 
     terms.push(""); 
     this.value = terms.join(", "); 
     $("#'.$first_id.'").val(this.value); 
     return false; 
    }, 
    minLength: 3, 

    }); 
}); 
+0

能否請你澄清你的問題:當自動完成是通過聽取close事件與任何關閉您可以刪除嗎? 「無焦點時取消ajax /自動完成」是什麼意思? – 2013-03-25 00:22:26

+0

當ajax腳本找不到任何東西,並且如果移動到下一個表單域時,輸入欄會保持灰色並顯示加載gif。我想要停止! – andymoyle 2013-03-25 07:27:35

+0

「當ajax腳本找不到任何東西時,」輸入字段保持灰色並帶有加載gif「jQuery Autocomplete不會使字段變灰或顯示加載gif。那些必須在你自己的代碼中,這是沒有顯示。 – Danack 2013-03-25 08:42:21

回答

0

我認爲autocomplete會自動爲您處理。示例實現在jQuery site顯示當焦點被髮送到別處時自動完成正在關閉。

有沒有可能是你有一些代碼無法正常關閉,阻止它?

反正 - 你可以調用close手動自動完成,但我想調查什麼是你的代碼首先發生。

$("#'.$first_id.'").on('blur', function(){ 
    $("#'.$first_id.'").autocomplete("close"); 
} 

編輯

從您的評論聽起來好像你要添加一些額外的CSS自動完成它被顯示時。

$(".selector").autocomplete({ 
    close: function(event, ui) {} 
}); 

$(".selector").on("autocompleteclose", function(event, ui) {});