dprh34164 2015-06-29 22:27
浏览 258
已采纳

使用html页面中的id将json数组值显示到列表中

iam working on mobile app. in my controller i created a json array and iam trying to display the array values into a list in my html page. i want show the list when html page is automatically loaded.

actually my json array contains 'category_id' $ 'category_name' and iam looking forward to next page when click on the 'category_name'

nextPage.html

 <!DOCTYPE html>
  <html>
  <head>
 <meta name="viewport" content="initial-scale=1, maximum-scale=1">

    <link rel="stylesheet" href="css/jquery.mobile-1.4.4.min.css" />
    <script src="js/jquery-1.11.1.min.js"></script>     
    <script src="js/jquery.mobile-1.4.4.min.js"></script>    
    <script src="js/nextPage.js"></script>    
   <title>Edfutura1</title>
  </head>
  <center>
 <body id="category_id">
 <div data-role="page" id="catlist"> 
 <div  id="loading" > </div>  
 <div data-role="header" data-position="fixed" data-theme="b">
 <h1>category</h1>
 </div> 
 <div data-role="main" class="ui-content">


            <ul data-role="listview" data-inset="true" id="category">
              <li></li>        
            </ul> 

        </div>

 </div>                 
 </body>
</center>
 </html>

nextPage.js

 var base_url="http://dev.edfutura.com/nithin/jps/edfuturaMob/";  
 $(document).on("pageinit","#catlist", function() {
 var submitUrl = base_url+"categorylist/get_categorylist"; 
 $("#loading").css("display", "block");
 $.ajax({
        url: submitUrl,

        dataType: 'json',
        type: 'POST',
        success: function(response)
        {
         //do something pls  
         },
         error: function() 
        {
            alert("error");

        }

    });





     });  

categorylist.php

 function get_categorylist() 
 {
   $cat=$this->categorylist_model->get_cat(); 


   echo json_encode($cat);  

  }

my json array

 [{"category_id":"1","category_name":"Academic Analysis"},         {"category_id":"2","category_name":"Teaching Analysis"},{"category_id":"3","category_name":"Skill Analysis"}]
  • 写回答

1条回答 默认 最新

  • dougu7546 2015-06-29 22:45
    关注

    You need to use the response in the $.ajax success function. This response object should be your json object and can therefore easily be accessed via the specified properties category_name and category_id.

    $.ajax({
        // ...
        success: function(response){
            // do something with the json response! E.g. generate a new list item and append it to the list.
            var categoryList = $('#category');
            var category;
            for(var i = 0, len = response.length; i < len; i++){
                category = response[i];
                categoryList.append($('<li>').attr('id', category.category_id).html(category.category_name));
            }
        },
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛