在jquery dropDown中自动完成

时间:2012-07-19 18:49:25

标签: javascript jquery-ui

我有一个dropdownList,其中包含我的客户的个人资料名称。

随着客户数量的增长,我需要一个自动完成功能,这样我就可以找到一个有建议的特定用户,而不是被迫在下拉列表中查找每个现有用户。

以下代码从数据库中提取数据:

$.getJSON(
            "profiles/custoomer.aspx?callback=?",
            {},
            function (data) {
                $.each(data, function (value, name) {
                    $('<option>').attr('value', value).text(name).appendTo('#customer_profile');
                });
            }
            );

如何添加自动填充功能?

3 个答案:

答案 0 :(得分:1)

您是否尝试使用自动完成组件? 这是他的文档,它易于使用且易于自定义!

http://jqueryui.com/demos/autocomplete/

答案 1 :(得分:0)

尝试使用以下示例,不要忘记包含Jquery最新文件;)享受Bro

<script>
    $(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"
        ];
        $( "#tags" ).autocomplete({
            source: availableTags
        });
    });
    </script>



<div class="demo">

<div class="ui-widget">
    <label for="tags">Tags: </label>
    <input id="tags">
</div>

</div><!-- End demo -->



<div style="display: none;" class="demo-description">
<p>The Autocomplete widgets provides suggestions while you type into the field. Here the suggestions are tags for programming languages, give "ja" (for Java or JavaScript) a try.</p>
<p>The datasource is a simple JavaScript array, provided to the widget using the source-option.</p>
</div><!-- End demo-description -->

答案 2 :(得分:0)

              $(function() {
                var availableTags = ["ribstar","major"];                    
                $( "#search" ).autocomplete({
                  source: availableTags,
                  select: function( event, ui ) 
                        { 
                              var $this = $(this).val(ui.item.label);
                              $('#sub_cat').children('[name="'+$this.val()+'"]').attr('selected', true);
                        }  
                });
              });

和html部分

<div class="ui-widget"><label for="tags">Search: </label><input type="text" name="search" id="search"></div>
<select name="sub_cat" id="sub_cat">
  <option value="1" name="ribstar">ribstar</option>
  <option value="2" name="major">major</option>
</select>