jQuery UI Autocomplete不显示结果

时间:2015-05-27 14:12:44

标签: php jquery mysql oop autocomplete

我正在尝试使用mysql查询的结果填充自动完成,并且选项永远不会显示,但我正确地获得了json响应。我试图在我的主css文件和jquery-ui.css中更改z-index但它不起作用。

这是我的jquery函数。

$('#tasklist').autocomplete({

    source: function(request, response) {

        $.ajax({

            url: 'pruebaproy.php',
            type: 'GET',
            data: {term: request.term},
            success: function(data) {

                response( $.map( data, function ( item ) {
                    return item;

            }));

            }

        });

    },
    minLength: 2,
    focus: function( event, ui ) {
        $('#tasklist').val( ui.item.nombre );
        return false;
    }

});

这是我执行mysql查询的PHP函数:

public function showTasks($term) {

    include 'Conexion.php';    
    $conectar = new Conexion();
    $arr_res = array();

    $consulta = "SELECT * FROM Actividades WHERE nombre LIKE '%".$term."%'";    

    if($stmt = $conectar->open()->query($consulta)) {

        while($row = $stmt->fetch_array(MYSQL_ASSOC)) {

            $task['id'] =  utf8_encode($row['idActividades']);
            $task['nombre'] =  utf8_encode($row['nombre']);
            $task['cat'] =  utf8_encode($row['parteAsoc']);

            array_push($arr_res, $task);

        }

        echo json_encode($arr_res);

    }

    $stmt->close();

}

我在'pruebaproy.php'中调用此函数

include('Proyecto.class.php');
$proyect = new Proyecto();


if(isset($_GET['term'])) {

    $proyect->showTasks(trim(strip_tags($_GET['term'])));

}

3 个答案:

答案 0 :(得分:0)

尝试在ajax请求中将数据类型更改为“json”,并在代码中放置一些警报以验证您是否到达那里(例如在成功内部)。

答案 1 :(得分:0)

$( "#city" ).autocomplete({
source: function( request,response ) {
    $.ajax({
      url: "dtautocomplete.php",
      dataType: "json",
        data: {term: request.term},

      success: function( data) {

   response( data.myData );

}


    });
  },
  select: function( ) {

 $( "#city" ).attr("selectflag","1");

}

});

Php side

<?php


//die("sleeeeeeeeeeeeeeeppppppppppppppppppppppppppppppppppppppppppppppp");
$term=$_GET['term'];
$query="SELECT `RegionName`, `CenterLatitude`,`CenterLongitude` FROM `regioncentercoordinateslist` WHERE `RegionName` LIKE '".$term."%' AND `RegionName` NOT LIKE '%type%' LIMIT 10";          
      // echo $query;
       $data= mysql_query($query);

       //var_dump($data);die();
       $count=0;
       while($dat=mysql_fetch_array($data)) 
       {
         if($count>12)
         {break;}


        $tmpt1=$dat['CenterLatitude'];
          $tmpt2=$dat['CenterLongitude'];
          $tmpt=$tmpt1.",".$tmpt2;
         $city[] = array(
        'label' =>$dat['RegionName'],
        'value' => $tmpt

    );
    $city2[] = $dat['RegionName'];
    $options['myData'][] = array(
    'label' =>$dat['RegionName'],
        'value' => $dat['RegionName']
); 



    $name=$dat['RegionName'];
//  echo "<div>$name</div>";
        $count++;

       }



      echo json_encode($options);
        //flush();


 ?>         

答案 2 :(得分:0)

尝试使用._renderItem。请参阅Custom data and display“viewsource”

$('#tasklist').autocomplete({    
    source: function(request, response) {    
        $.ajax({    
            url: 'pruebaproy.php',
            type: 'GET',
            data: {term: request.term},
            success: function(data) {    
                response( $.map( data, function ( item ) {
                    return item
                })    
            }    
        });    
    },
    minLength: 2,
    focus: function( event, ui ) {
        $('#tasklist').val( ui.item.nombre );
        return false;
    }
}).autocomplete( "instance" )._renderItem = function( ul, item ) {
  return $( "<li>" )
    .append( "<a>" + item.id + "<br>" + item.cat + "</a>" )
    .appendTo( ul );
  };
});

<head>
  <meta charset="utf-8">
  <title>jQuery UI Autocomplete - Default functionality</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
  <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
    }).autocomplete("instance")._renderItem = function(ul, item) {
      console.log(ul, item)
      return $("<li>")
        .append("<b style=color:orange;>" + item.label + "</b>")
        .appendTo(ul);
    };
    });
  </script>
</head>

<body>

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


</body>