jQuery-UI自动完成数据未显示

时间:2017-10-16 13:44:10

标签: javascript jquery ajax jquery-ui autocomplete

jQuery的:

function search($id, $column) {
  $("#" + $id).autocomplete({
    source: function(request, response) {
      jQuery.post("<?php echo base_url().'index.php/Auto_complete/item_search'?>", {
        Query: request.term,
        column: $column
      }, function(data) {
        response(data);
      });
    }
  });
}

HTML:

<input type="text" class="form-control English_name" id="English_name" name="English_name" placeholder="Enter Item Name (English)" onchange="product_name()" onkeyup="search('English_name', 'English_name')" autocomplete="off">

AJAX输出:

["Styler King 6 Card Holder  (Set of 1, Khaki)","ds 18 sticker","famous sticker","wx sticker","lk small sticker","LX Sticker"]

任何人都可以帮助我,为什么数据没有显示,我如何在jQuery UI自动完成中显示数据?

1 个答案:

答案 0 :(得分:0)

您不清楚自己在尝试使用代码完成的任务。

以下是您可能会发现有用的示例:

https://jsfiddle.net/Twisty/t1ruyg11/

<强>的JavaScript

var myData = [
  "Styler King 6 Card Holder  (Set of 1, Khaki)",
  "ds 18 sticker",
  "famous sticker",
  "wx sticker",
  "lk small sticker",
  "LX Sticker"
];

$(function() {
  $("#English_name").autocomplete({
    source: function(req, resp) {
      $.post("/echo/json/", {
        json: JSON.stringify(myData)
      }, function(data) {
        resp($.ui.autocomplete.filter(data, req.term));
      });
    }
  });
});

现在,考虑到您的代码,您可能需要尝试以下内容:

function search(t, c){
  var url = "<?php echo base_url().'index.php/Auto_complete/item_search'; ?>";
  var query = {
    Query: t.trim(),
    column: c
  };
  $.post(url, query, function(results){
    if(results.length){
      return results;
    } else {
      return [];
    }
  });
}

$(function(){
  $("#English_name").autocomplete({
    source: function(request, response){
      response(search(request.term), $column);
    }
  });
});