3

我有一个下拉列表,其中包含我的客户的个人资料名称。

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

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

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

如何添加自动完成功能?

4

3 回答 3

1

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

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

于 2012-07-19T18:56:51.413 回答
0

尝试使用下面的示例,不要忘记包含Jquery最新文件;) Njoy 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 -->
于 2012-07-20T12:54:52.250 回答
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>
于 2013-11-14T15:50:30.560 回答