MAO-EYE 2017-08-14 09:42 采纳率: 100%
浏览 17

自动完成不给结果

I have problem with autocomplete script. Script don't give any names under input. Here is index:

<head>
        <html lang="en">
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <title>jQuery UI Autocomplete - Multiple values</title>
      <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
      <link rel="stylesheet" href="/resources/demos/style.css">
      <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
      <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

      <script>

      function getData(){
      var userName = document.getElementById("tags");
      var string = userName.value;
         $.ajax({
                  method: 'POST',
                  url: 'bla.php',
                  data: {
                    str: string
                  },
                  success: function(content) {
                    console.log("Content: " + content);

         var availableTags = content;
       function split( val ) {
          return val.split( /,\s*/ );
        }
        function extractLast( term ) {
          return split( term ).pop();
        }

        $( "#tags" ).autocomplete({
            minLength: 0,
            source: function( request, response ) {
              // delegate back to autocomplete, but extract the last term
              response( $.ui.autocomplete.filter(
                availableTags, extractLast( request.term ) ) );
            },
            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;
            }
          });
                  },
                  error: function(xhr, status) {
                    console.log("ERROR");
                  }
               });


      } 
      </script>
    </head>
    <body>

    <div class="ui-widget">
      <label for="tags">Names: </label>
      <input id="tags" size="50" onKeyDown="getData();">
    </div>


    </body>
    </html>

And here is bla.php Connect to db is correct, data from Db is correct.

    $d = new Database();
$d->query("SELECT u.cele_jmeno, u.id FROM uzivatele u WHERE u.cele_jmeno LIKE CONCAT(:str,'%') ORDER BY u.cele_jmeno LIMIT 0,15");   
 $d->bind(":str", $_POST["str"]);
 $vysledky = $d->resultset();
 $res = Array();
 $num = 0;
 foreach($vysledky AS $vysledek){
    $res[$num] = $vysledek["cele_jmeno"];
    $num++;

 }  
    echo  json_encode($res);

In ajax I have console log, which return coorect values. For example If I inser into input Ter, then in console.log i Have: Content: ["Terrence Rowell","Terry Moony","Terry Morco"] and if I insert Terry i have only Content: ["Terry Moony","Terry Morco"]. It's all correct data, but, script nothing write into autocomplete box. When, I have data in array in javascrit, all works, but if I insert array from php script i have no autocomplete box.

Here is screenshot: Box when I write first letter

  • 写回答

2条回答 默认 最新

  • 撒拉嘿哟木头 2017-08-14 10:30
    关注

    Because you return a json encode value so split function work as letter by letter instead one string...So need to give dataType as json.

    Add data type into your ajax request for ex: dataType: "json", like this

    $.ajax({
    method: 'POST',
    url: 'bla.php',
    dataType: "json",
    data: {
          str: string
    }, etc...
    

    My test1.php

    <head>
            <html lang="en">
          <meta charset="utf-8">
          <meta name="viewport" content="width=device-width, initial-scale=1">
          <title>jQuery UI Autocomplete - Multiple values</title>
          <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
          <link rel="stylesheet" href="/resources/demos/style.css">
          <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
          <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
    
          <script>
    
          function getData(){
          var userName = document.getElementById("tags");
          var string = userName.value;
             $.ajax({
                      method: 'POST',
                      url: 'test.php',
                      dataType: "json",
                      data: {
                        str: string
                      },
                      success: function(content) {
                        console.log("Content: " + content);
    
             var availableTags = content;
           function split( val ) {
              return val.split( /,\s*/ );
            }
            function extractLast( term ) {
              return split( term ).pop();
            }
    
            $( "#tags" ).autocomplete({
                minLength: 0,
                source: function( request, response ) {
                  // delegate back to autocomplete, but extract the last term
                  response( $.ui.autocomplete.filter(
                    availableTags, extractLast( request.term ) ) );
                },
                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;
                }
              });
                      },
                      error: function(xhr, status) {
                        console.log("ERROR");
                      }
                   });
    
    
          } 
          </script>
        </head>
        <body>
    
        <div class="ui-widget">
          <label for="tags">Names: </label>
          <input id="tags" size="50" onKeyDown="getData();">
        </div>
    
    
        </body>
        </html>
    

    and my test.php

    <?php 
    $vysledky = ["Terry Moony","Terry Morco","window","Libertéz"];
    $num = 0;
    foreach($vysledky AS $vysledek){
        //print_r($vysledek);
        $res[$num] = $vysledek;
        $num++;
    
     }  
    
    echo  json_encode($res);
    
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型