简体   繁体   English

如何在JavaScript中将JSON数据附加到HTML?

[英]How to append the JSON data to HTML in JavaScript?

I get some data using JSON array. 我使用JSON数组获取了一些数据。 I want to append each data in a div. 我想将每个数据附加到div中。 But I don't get what's wrong in that? 但是我不明白这是怎么回事?

controller 控制者

   function get_performers()
   {
    $id = $this->input->post('id'); 
    $exam = $this->input->post('exam'); 
    $datas=$this->job->get_top_ten_st_data($id,$exam);
    $out['student_details'] = $datas;
     echo json_encode($out); 
   }

script 脚本

function get_performers(id,exam)
{

    $.ajax({
        url:"<? echo base_url();?>class_analysis/get_performers", 
        dataType: 'json',
        type: "POST",
        data: {id:id,exam:exam},      
        success:function(result) {  
            // alert("haii");
            console.log(result);
            result = JSON.parse(result);

          var tab= "<div class='col-xs-2 blk-ht'>  <span class='hd'>Names</span> </div>";
           for(var i=0;i<result.student_details.length;i++)
            {

                 tab=tab+"<div class='col-ds-1'><span class='subjNames'>" + result.student_details[i]["subject_name"]+ "</span></div> ";
            }   

           jQuery("#subjectNames").append(tab);

        }
    }); 
}

Any problem in this? 有什么问题吗?

Try html not append 尝试html附加

  jQuery("#subjectNames").html(tab);

Also if jQuery("#subjectNames") equal with null in your console this mean that you don't have element with id="subjectNames" in html not id="#subjectNames" or other. 另外,如果jQuery("#subjectNames")在控制台中等于null ,则意味着您没有HTML中带有id="subjectNames"元素,而不是id="#subjectNames"或其他id="#subjectNames" May be you use classes then try $(".subjectNames") with . 可能是您使用类,然后尝试使用$(".subjectNames") . not # 不是#

The Loop should work... Seems to be another problem with your result. 循环应该工作...结果似乎是另一个问题。

 var tab= "<div class='col-xs-2 blk-ht'><span class='hd'>Names</span> </div>"; for(var i=0;i<20;i++) { tab=tab+"<div class='col-ds-1'><span class='subjNames'>" + "test: " + i + "</span></div> "; } jQuery("#subjectNames").append(tab); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="subjectNames"><div> 

dataType: 'json' in $.ajax options - automaticaly parse your json $ .ajax选项中的dataType:'json'-自动解析您的json

and USE JQUERY ))) 和使用JQUERY)))

IN SUCCES AJAX FUNCTION 成功的AJAX功能

$.each( result.student_details, function( key, value ) {
      alert( key + ": " + value );
});
<?php 
function get_performers()
{
    $id = $this->input->post('id'); 
    $exam = $this->input->post('exam'); 
    $datas=$this->job->get_top_ten_st_data($id,$exam);
    $out['student_details'] = $datas;
    echo json_encode($out); 
}
?>
<script>
$.ajax({
    url:"<? echo base_url();?>class_analysis/get_performers", 
    dataType: 'json',
    type: "POST",
    data: {id:id,exam:exam},      
    success:function(result) {

        $.each(response.student_details, function(key,value){
             appendHtml(key,value);
        });


    }
}); 

function appendHtml(key,value)
{
    var tab= "<div class='col-xs-2 blk-ht'>  <span class='hd'>Names</span> </div>";
    tab = tab+"<div class='col-ds-1'><span class='subjNames'>" +value+ "</span></div> ";
    jQuery("#subjectNames").append(tab);
}

</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM