jquery autocomplete select messing data

jquery autocomplete select messing data

本文关键字:data messing select autocomplete jquery      更新时间:2023-09-26

我不知道为什么在jquery自动完成时,select函数会弄乱数据。我的意思是value = label不应该的时候。

console.log(val);的控制台日志输出

Object { value="1558825", label="Cree sus propias noticias cliente", icon="http://servidor...News/10_ae4e0.jpg"}

console.log(ui.item);的控制台日志输出

Object { label="Cree sus propias noticias cliente", value="Cree sus propias noticias cliente"}

法典:

$("#search_input").autocomplete({
  source: function(req, add) {
    $.getJSON("do.php", { OP: "news_search", category: cat_id, get: req }, function(results){
      var suggestions = [];
      $.each(results, function(i, val){
        console.log(val);
        suggestions.push(val.label)
      });
      add(suggestions);
    });
  },
  select: function(event, ui){
    console.log(ui.item); // Here value and label is the same, when it shouldn't
    $("#search_input").val(ui.item.label).attr('data-target', ui.item.value);
    return false;
  },
  minLength: 2
});

任何想法为什么会这样?

代码suggestions.push(val.label)只推送标签。因此,除了ui对象中的标签外,您无法获得任何其他内容

尝试将 val 对象推送suggestions.push(val)