jQuery自动完成选择事件不起作用

时间:2012-07-25 09:43:19

标签: jquery jquery-ui jquery-autocomplete

我在我的网站上使用jQuery auto complete。我想在用户点击下拉选项时转到搜索结果页面。为此我使用了“选择”事件,但在选择事件期间它不会触发。

有什么想法吗?这是代码

 $(function(){
var availableTags = [
            "ActionScript",
            "AppleScript",
            "Asp",
            "BASIC",
            "C",
            "C++",
            "Clojure",
            "COBOL",
            "ColdFusion",
            "Erlang",
            "Fortran",
            "Groovy",
            "Haskell",
            "Java",
            "JavaScript",
            "Lisp",
            "Perl",
            "PHP",
            "Python",
            "Ruby",
            "Scala",
            "Scheme"
        ];



         $('#autocomplete').autocomplete({
            source: availableTags,
                select: function(event,ui){
                    alert("Selected ");
                }
         });

1 个答案:

答案 0 :(得分:3)

您错过了$(function() {的右括号 - 您需要在结尾处添加}); ..

$(function() {
    var availableTags = [
        "ActionScript",
        "AppleScript",
        "Asp",
        "BASIC",
        "C",
        "C++",
        "Clojure",
        "COBOL",
        "ColdFusion",
        "Erlang",
        "Fortran",
        "Groovy",
        "Haskell",
        "Java",
        "JavaScript",
        "Lisp",
        "Perl",
        "PHP",
        "Python",
        "Ruby",
        "Scala",
        "Scheme"
        ];
    $('#autocomplete').autocomplete({
        source: availableTags,
        select: function(event, ui) {
            alert("Selected ");
        }
    });
});​  // added this

Then it works fine

还要确保添加了正确的JS库和CSS

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script src="http://code.jquery.com/ui/1.8.22/jquery-ui.min.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/smoothness/jquery-ui.css">