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 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退