【问题标题】:Autocomplete Jquery get id and name自动完成 Jquery 获取 id 和 name
【发布时间】:2016-08-15 10:55:30
【问题描述】:

我是这方面的新手,需要一些帮助,从我的数据库中使用自动完成功能获取游戏的 ID 和名称

一切正常,除了我无法获得 id。

我有一个 get_games.php,看起来像这样

while ($row = mysql_fetch_assoc($data))
{        
    $results[$row['id']] =  $row['title'];
}

echo json_encode($results);

我的 Jquery

  <script>
  $(function() {
    $( "#games" ).autocomplete({
      source: 'get_games.php',
      select: function( event, ui ) {
        $( "#id" ).val( ui.item.id );
        return false;
      }
    });
  });
  </script>

HTML

<input id="games" />
<input id="id" />

我在游戏输入中看到所有标题,但没有 id。

【问题讨论】:

  • 什么时候使用 ui.item.value 我得到了“标题”
  • label 也给了我标题,我的数组有问题吗?{"1":"game1","2":"game2","69":"game3"} 看起来像这样编码后

标签: php jquery autocomplete


【解决方案1】:

生成具有属性valueid值)和labeltitle值)的对象数组,然后使用ui.item.value获取它。

PHP:

while ($row = mysql_fetch_assoc($data))
{        
    $results[] =  array(
         'label' => $row['title'],
         'value' => $row['id'],
        );
}

echo json_encode($results);

jQuery :

$(function() {
   $( "#games" ).autocomplete({
      source: 'get_games.php',
      select: function( event, ui ) {
        $( "#id" ).val( ui.item.value );
        return false;
      }
   });
});

【讨论】:

  • 你是最棒的!谢谢你:)
  • @SKSK :很高兴为您提供帮助 :)
猜你喜欢
  • 2012-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-09
  • 1970-01-01
  • 2014-07-04
相关资源
最近更新 更多