【问题标题】:Jquery UI Autocomplete with JSON not working使用 JSON 的 Jquery UI 自动完成功能不起作用
【发布时间】:2013-04-03 17:45:27
【问题描述】:

大家好,我想弄清楚为什么当我在源代码末尾添加一个变量时,autocomplete() 方法没有执行GET。例如:

    <script> 
    $(document).ready(function(){
        var search_input;

        $('#search').keyup(function(){
            search_input = ($(this).val());
            console.log(search_input);
        });
        $('#search').autocomplete({
          source: "http://192.168.33.10/app_dev.php/search/query/" + search_input,
          minLength: 2
        });
    });
    </script>

    <div class="ui-widget">
        <label for="search">Search</label>
        <input type="text" id="search" />
    </div>   

但是,如果我从源中删除 + search_input,它将执行 GET,就像这样..

    <script> 
    $(document).ready(function(){
        var search_input;

        $('#search').keyup(function(){
            search_input = ($(this).val());
            console.log(search_input);
        });
        $('#search').autocomplete({
          source: "http://192.168.33.10/app_dev.php/search/query/",
          minLength: 2
        });
    });
    </script>

    <div class="ui-widget">
        <label for="search">Search</label>
        <input type="text" id="search" />
    </div>   

【问题讨论】:

    标签: jquery ajax json jquery-ui jquery-ui-autocomplete


    【解决方案1】:

    在您的代码中,autocomplete 函数只需要为 source 参数提供一个回调函数。 source: function( request, response ) {}

    您可以按照Multiple remote suggestions on jQuery UI Autocomplete的示例进行操作

        <script type="text/javascript"> 
          $(function() {
        function split( val ) {
          return val.split( /,\s*/ );
        }
        function extractLast( term ) {
          return split( term ).pop();
        }
    
        $( "#search" )
          // don't navigate away from the field on tab when selecting an item
          .bind( "keydown", function( event ) {
            if ( event.keyCode === $.ui.keyCode.TAB &&
                $( this ).data( "ui-autocomplete" ).menu.active ) {
              event.preventDefault();
            }
          })
          .autocomplete({
            source: function( request, response ) {
              $.getJSON( "http://192.168.33.10/app_dev.php/search/query/" + extractLast( request.term ), {
                term: extractLast( request.term )
              }, response );
            },
            search: function() {
              // custom minLength
              var term = extractLast( this.value );
              if ( term.length < 2 ) {
                return false;
              }
            },
            focus: function() {
              // prevent value inserted on focus
              return false;
            },
            select: function( event, ui ) {
              var terms = split( this.value );
              // remove the current input
              terms.pop();
              // add the selected item
              terms.push( ui.item.value );
              // add placeholder to get the comma-and-space at the end
              terms.push( "" );
              this.value = terms.join( ", " );
              return false;
            }
          });
      });
            </script>
    

    本例中服务器返回的 JSON 结构如下:

    [{"id":"1544","label":"Suggestion 1","value":"Suggestion 1"},
    {"id":"3321","label":"Suggestion 2","value":"Suggestion 2"}]
    

    【讨论】:

    • 我还有一个问题。我想用jqueryui.com/autocomplete/#categories 之类的标签对搜索结果进行分类。我很困惑如何将该代码集成到您提供的工作代码中。
    【解决方案2】:

    即使您正在更改search_input,小部件也只会获取初始值为search_input(可能为空)的URL。

    你需要这样的东西:

    $('#search').autocomplete({
        source: function (request, response) {
            $.get("http://192.168.33.10/app_dev.php/search/query/" + request.term, response)
        },
        minLength: 2
    });
    

    【讨论】:

    • 谢谢!我在下拉列表中得到了正确的 JSON 响应,但格式很奇怪。它实际上是从 JSON 中提取每个字符,尽管包括 [ { : " 并且键一次命名一个字符。知道为什么会这样吗?
    • 您能否提供来自您的服务的示例响应?您也可以尝试改用getJSON
    猜你喜欢
    • 2018-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-26
    • 2012-12-16
    • 1970-01-01
    相关资源
    最近更新 更多