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

display json array values into a list using its id in a html page

我正在开发移动应用程序。在我的控制器中,我创建了一个 json 数组,我试图将数组值显示到我的 html 页面中的列表中。我想在 html 页面自动加载时显示列表。

实际上我的 json 数组包含 'category_id' $ 'category_name' 我很期待单击 '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);  

  }

我的json数组

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

您需要在$.ajax成功函数中使用response。此 response 对象应该是您的 json 对象,因此可以通过指定的属性 category_namecategory_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));
        }
    },
});