2012-02-19 3 views
2

아래에 이미지가있는 Jquery 자동 완성 메뉴가 있습니다. 아래 코드를 추가했습니다.Jquery 자동 완성 닫기 함수

그림을 클릭 한 후 Jquery Autocomplete 메뉴가 닫히는 (사라지게 할) 방법이 있습니까?

도움 주셔서 감사합니다.

<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")'는 실제로 아무것도 닫지 않았다. –

+0

@AndrewShepherd 아니, 나는하지 않았다. – alex

+0

다음은 jsfiddle 예제입니다. http://jsfiddle.net/ZqGYq/ –

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); 
      }, 
    }); 
}); 



​