Jquery自动完成关闭功能

时间:2012-02-19 00:45:52

标签: jquery autocomplete

我有一个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 个答案:

答案 0 :(得分: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

答案 1 :(得分: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);
            },
    });
}); 



​