jquery autocomplete不显示结果

时间:2014-06-12 09:54:40

标签: javascript php jquery autocomplete

我输入了附加自动填充的类型文本:

的javascript:

$("#descSearchBox").keyup(function(e) {
//$("#ui-id-1").addClass("dropdown-menu").removeClass('ui-autocomplete ui-front ui-menu ui-widget ui-widget-content ui-corner-all');

$(".ui-autocomplete").removeClass("ui-autocomplete ui-front ui-menu ui-widget ui-widget-content ui-corner-all")
    .addClass('dropdown-menu');

$.post("/controls/DistControl.php", {
        action: 'descAutocomplete',
        description: $(this).val(),
        myDistribution: $("#cbMyDistributions").is(":checked")
    }, function(data) {

        data = $.parseJSON(data);

        $( "#titleSearchBox" ).val('');
        console.log(data);

        $( "#descSearchBox" ).autocomplete({
            source: data.result.titles,
            select: function( event, ui ) {
                $("#titleSearchBox").val(ui.item.label);

                distributionTableUpdate();
            }
        });
    }
);
});

这里是发送请求的php脚本:

function descAutocomplete() {
$where['LIKE'] = [ "description" => $_POST['description'] ];

if( $_POST["myDistribution"] === "true" )
    $where["user_id[=]"] = $_SESSION['user_id'];

$dists = DistributionModel::getAll( $where, 10, 0, 'title', 'ASC', [ 'title' ] );

$titles = [];
foreach($dists as $d) {
    $titles[] = $d->get('title');
}

$response = new ResponseModel(ResponseModel::$TYPE_INFO, 'Description Autocomplete', [ 'titles' => $titles ]);
echo $response->encode();
}

问题是我得到的结果,例如我只是输入' e'在搜索中,我得到4个元素的数组,但它没有显示任何内容或只显示最后一个元素。为什么会这样?为什么它没有显示所有结果?

1 个答案:

答案 0 :(得分:0)

尝试使用Like

$("#name").autocomplete({
  source: function( request, response ) {
    $.ajax({
      url: "URL",
      dataType: "json",
      data: {term: request.term},
      success: function(data) {
        response($.map(data, function(item) {
          return {
            label: item.state,
            id: item.id
          };
        }));
      }
    });
  },
  select: function(event, ui) {
    $('#id').val(ui.item.id);
  }
});