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));
            }
        },
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?