Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 自动完成Jquery获取id和名称_Php_Jquery_Autocomplete - Fatal编程技术网

Php 自动完成Jquery获取id和名称

Php 自动完成Jquery获取id和名称,php,jquery,autocomplete,Php,Jquery,Autocomplete,我对这一切都是新手,需要一些帮助,通过autocomplete从我的数据库中获取游戏的id和名称 一切正常,除了我拿不到身份证 我有一个get_games.php,看起来像这样 while ($row = mysql_fetch_assoc($data)) { $results[$row['id']] = $row['title']; } echo json_encode($results); 我的Jquery <script> $(functi

我对这一切都是新手,需要一些帮助,通过autocomplete从我的数据库中获取游戏的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>

$(函数(){
$(“#游戏”)。自动完成({
来源:“get_games.php”,
选择:功能(事件、用户界面){
$(“#id”).val(ui.item.id);
返回false;
}
});
});
Html



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

使用属性
id
值)和
标签
title
值)生成对象数组,然后使用
ui.item.value

PHP:

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

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

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

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

使用属性
value
id
value)和
label
title
value)生成对象数组,然后使用
ui.item.value
获取该数组

PHP:

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

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

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

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

当使用ui.item.value时,我得到的“title”标签也给了我标题,我的数组有问题吗?{“1”:“game1”,“2”:“game2”,“69”:“game3”}编码后是这样的当使用ui.item.value时,我得到的“title”标签也给了我标题,我的数组有问题吗?{“1”:“game1”,“2”:“game2”,“69”:“game3”}看来你是最棒的!谢谢:)@SKSK:很高兴能帮上忙:)你是最棒的!谢谢:)@SKSK:很乐意帮忙:)