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

如何在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 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)