自动完成源取决于下拉选择

时间:2013-03-21 21:27:30

标签: javascript jquery

您好我正在尝试实现一些JQuery来使用自动完成UI。我有一个有两个选项的下拉列表。演员和电影。根据所选内容,我希望自动完成输入框的源不同。我有JQuery有什么问题吗?

<script type="text/javascript">
           $(document).ready(function(){
           $("#selectType").change(function() {

           if ($(this).val() == "Actor"){
            $("#tags").autocomplete({
            source: "nameSearch.php",
            minLength: 2
        });

        }
        else if($(this).val() == "Film"){
            $("#tags").autocomplete({
            source: "FilmSearch.php",
            minLength: 2
        });
        }
        });
        });
    </script>

1 个答案:

答案 0 :(得分:2)

像这样使用:

       $(document).ready(function () {
           //Create widget with default data source
           $("#tags").autocomplete({
               source: "nameSearch.php",
               minLength: 2
           });

           $("#selectType").change(function () {
               // Assign new data source like that :

               if ($(this).val() == "Actor")
                   $("#tags").autocomplete("option", "source", "nameSearch.php");
               else 
                   if ($(this).val() == "Film")
                       $("#tags").autocomplete("option", "source", "FilmSearch.php");

              // And what is your 2 conditions are not met?????
           });
       });