1

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
});

なぜそれが起こっているのですか?

4

1 に答える 1

1

コード suggestions.push(val.label)はラベルのみをプッシュします。uiしたがって、オブジェクトのラベル以外は取得できません

val オブジェクトをプッシュしてみてくださいsuggestions.push(val)

于 2012-05-23T12:58:44.497 に答える