2012-02-19 198 views
2

我有一個Jquery自動完成菜單,底部有一個圖像 - 添加了下面的代碼。Jquery自動完成關閉功能

在點擊圖片後,有沒有辦法讓jquery自動完成菜單關閉(消失)?

謝謝你的幫助。

<script> 
$(function() { 
     var availableTags = [ 
     "new york, ny", 
     "new york city, ny", 
     "manhattan, ny", 
     "queens, ny", 
     "brooklyn, ny", 
     "bronx, ny", 
     "staten island, ny", 
     "kings county, ny", 
     "richmond county, ny", 
     "big apple, ny" 
    ]; 
    $("#tags").autocomplete({ 
     source: availableTags, 
     open: function (event, ui) { 
        var nyc="<img src=\"images/iloveny.jpg\" onclick=\"DoGood()\"/>"; 
        $(this).autocomplete("widget").append(nyc); 
      }, 
    }); 
}); 
</script> 

回答

2

在點擊事件中,請致電$("#tags").autocomplete("close");

這裏是我會做...

$("#tags").autocomplete({ 
    source: availableTags, 
    open: function(event, ui) { 

     var nyc = $("<img />", { 
      src: "http://www.gravatar.com/avatar/8a3efc8be996c87da020df31e2416151?s=32&d=identicon&r=PG", 
      alt: "" 
     }); 

     nyc.click(function() { 
      $(event.target).autocomplete("close"); 
     }); 

     $(this).autocomplete("widget").append(nyc); 
    } 
});​ 

jsFiddle

+0

其實,你測試了這個?我試過了,'ui.autocomplete(「close」)'實際上並沒有關閉任何東西。 – 2012-02-19 01:40:30

+0

@AndrewShepherd不,我沒有。 – alex 2012-02-19 01:42:43

+0

這是一個jsfiddle的例子:http://jsfiddle.net/ZqGYq/ – 2012-02-19 01:44:09

0

我是這樣做的:

function closeAutocomplete() { 
    $("#tags").next().hide(); 
} 


$(function() { 
     var availableTags = [ 
     "new york, ny", 
     "new york city, ny", 
     "manhattan, ny", 
     "queens, ny", 
     "brooklyn, ny", 
     "bronx, ny", 
     "staten island, ny", 
     "kings county, ny", 
     "richmond county, ny", 
     "big apple, ny" 
    ]; 
    $("#tags").autocomplete({ 
     source: availableTags, 
     open: function (event, ui) { 
        var nyc="<img id=\"nycImage\" src=\"https://twimg0-a.akamaihd.net/profile_images/772597407/DCA_Twitter_normal.png\" />"; 

        $(this).autocomplete("widget").append(nyc); 
        $('#nycImage').click(closeAutocomplete); 
      }, 
    }); 
}); 



​