dongxia9620 2014-01-02 01:46
浏览 479
已采纳

如何在json中获取第二个数组?

I have use auto complete with json and php. it works, but now I need to get the second array from the json.

This my script on source.php

<?php

$req = "SELECT namaBarang, harga FROM masternamabarang WHERE namaBarang LIKE '%".$_REQUEST['term']."%' ";

       $query = mysql_query($req);

       while($row = mysql_fetch_array($query))
       {
           $results[] = array('label' => $row['namaBarang'],'harga' => $row['harga']);
       }

       echo json_encode($results);  
?>

and this script for autocomplate.php

<html>
   <head>

    <script src="js/jquery-1.9.1.js"></script>
    <script src="json/js/jquery-ui-1.10.3.js"></script>
    <script>
      $(function() 
      {
       $( "#NAMA" ).autocomplete({
           source: 'source.php'
      });
      });
    </script>

    <script>
    function ambil()
    {
    var x = document.getElementById('NAMA').value;

    document.getElementById('HARGA').value=x;   
    }
    </script>
</head>
<body>
  <form>
    <input type="text" name="NAMA" size="50" id="NAMA" onchange="ambil()"/><br>
    <input type="text" name="HARGA"  size="50"  id="HARGA" />

  </form>
</body>

The Autocomplete works well, but ALL I need is, take the second array[HARGA] from JSON array and fill it on text box HARGA.

I appreciate your answers.

  • 写回答

2条回答 默认 最新

  • dongshan1811 2014-01-02 07:24
    关注

    your script.php works fine. It give you json result something like

    [{"label":"laba","value":"vala"},{"label":"labb","value":"valb"}]
    

    you can log (javascript) result in browser like this

    console.log(data[0].value); // array index 0 for first row, you can try 1,2 etc...
    

    you probably need select method to bind what was selected:

    $( "#search" ).autocomplete({
          source: data,
          select: function( event, ui ) {
            console.log(ui); // log selected data row
            $('#mySelectedInput').val(ui.item.value); // bind value wherever you like
          }
     });
    

    resources
    http://api.jqueryui.com/autocomplete/#event-select
    http://jqueryui.com/autocomplete/#custom-data

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据