2013-03-24 101 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) {});