duan1983 2012-08-13 07:22
浏览 47
已采纳

如何在带链接的列表中获取json数据

Hi i am able to get json data as a list in html page but i like to get as a list of link. Because when i click on the link it will show details.

What i have done in tenantlistmob.php

<?php 
include('connection.php');
$result = mysql_query("SELECT * FROM tenanttemp");
while ($row = mysql_fetch_assoc($result)) 
{
$array[] = array($row['TenantFirstName']);
}
echo json_encode($array);
?> 

Then my html page is

<!DOCTYPE HTML>
<html>
<link rel="stylesheet" href="../jasmine-device_2/styles/main.css" />
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript" language="javascript">
 $(document).ready(function() { 
    jQuery.getJSON("tenantlistmob.php", function (jsonData) {

            jsonData= eval(jsonData);//get json array

            for (i = 0; i < jsonData.length; i++)//iterate over all options
            {
              for ( key in jsonData[i] )//get key => value
              { 
             $("#getname").append($("<li></li>").html(jsonData[i][key]), document.all ? i : null);
              }
            }

});

 });
</script>
</head>
<body>
<form name="index">
<div id="getname"></div>
</form>
</body>
</html>

The output is

 Humayun
 Sahjahan
 Bayezid
 Bayezid
 Asaduzzaman
 Mouri

I am getting the TenantFirstName as a list.But i like to get it as a list of link.Because when i click on a name it will show details of that name. How can i do both work(as a list of link and when click on a link it will show details in a html page query from mysql database)? Please help.

  • 写回答

1条回答 默认 最新

  • doudu8291 2012-08-13 08:24
    关注

    use table like below

    <!DOCTYPE HTML>
    <html>
    <link rel="stylesheet" href="../jasmine-device_2/styles/main.css" />
    <head>
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script type="text/javascript" language="javascript">
    $(document).ready(function() { 
        jQuery.getJSON("tenantlistmob.php", function (jsonData) {
    
               $("#user_spec").html("");//clear old options
    
                jsonData= eval(jsonData);//get json array
    
                for (i = 0; i < jsonData.length; i++)//iterate over all options
                {
                  for ( key in jsonData[i] )//get key => value
                  { 
                        //$("#user_spec").get(0).add(new Option(jsonData[i][key],[key]), document.all ? i : null);
                        //$("#getname tbody").append($("<li></li>").html(jsonData[i][key]), document.all ? i : null);
                        var tblRow = "<tr>" + "<td>" + "<a href='#'>" + jsonData[i][key] + "</a>" + "</td>" + "</tr>"
                        $(tblRow).appendTo("#getname tbody");
                  }
                }
    
      });
    
     });
     </script></head>
     <body>
     <form name="index">
     <div>
     <table id="getname" border="1">
        <thead>
            <th>Name</th>
        </thead>
      <tbody>
    
       </tbody>
    </table>
    </div>
    </form>
    </body>
    </html> 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 经gamit解算的cors站数据再经globk网平差得到的坐标做形变分析
  • ¥15 GD32 SPI通信时我从机原样返回收到的数据怎么弄?
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!