如何在HTML上打印JSON的多行返回

时间:2016-01-06 11:33:51

标签: php html json

如何按照我的js功能打印JSON多行返回

function getSheduleDate()
{
    //alert("enters1");
    //var usrname= getParameterByName('uname');
    var postVal=$.post('http://localhost/ipack/salesvisit.php',{'USERNAME':$("#username").val()},
    function (jsonVal)
    {
        //alert("enters");
        obj = JSON.parse(jsonVal);
        //console.log(obj.VISIT_DATE+"---"+obj.LEADNAME);
        obj = JSON && JSON
        .parse(jsonVal) || $.parseJSON(jsonVal);
});
}

JSON结果,

{"VISIT_DATE":"04\/01\/2016 11:00:00","0":"04\/01\/2016 11:00:00","LEADNAME":"test 4","1":"test 4"}
{"VISIT_DATE":"05\/01\/2016 12:15:00","0":"05\/01\/2016 12:15:00","LEADNAME":"TEST","1":"TEST"}

我想在HTML中打印多行。我有一个关于我的HTML的手风琴,我想打印到'事件'在我的手风琴上显示。

<div class="accordion">
<div class="accordion-section">
    <a class="accordion-section-title" href="#accordion-1">
        <script >
            var m_names = ["January", "February", "March",
                "April", "May", "June", "July", "August", "September",
                "October", "November", "December"];

            var d_names = ["Sunday","Monday", "Tuesday", "Wednesday",
                "Thursday", "Friday", "Saturday"];

            jQuery(document).ready(function(){
                var i = 0;
                jQuery('.accordion-section a').each(function(){
                    var myDate = new Date();
                    myDate.setDate(myDate.getDate() + i );
                    var curr_date = myDate.getDate();
                    var curr_month = myDate.getMonth();
                    var curr_day  = myDate.getDay();
                    jQuery(this).html(d_names[curr_day] + " - " + m_names[curr_month] + " " +curr_date);
                    i++;
                });
            });
        </script>
    </a>

    <div id="accordion-1" class="accordion-section-content">
        <div>
            <ul>
                <li>Event 1</li>
                <li>Event 2</li>
                <li>Event 3</li>
            </ul>
        </div>


    </div><!--end .accordion-section-content-->
</div><!--end .accordion-section-->

请帮帮我......

2 个答案:

答案 0 :(得分:0)

您可以使用jQuery的$.each

$.each(JSON.parse(jsonVal), function(index, obj){
   var eventItem = $("<li></li>").text("<a href='http://google.com/'>"+obj.LEADNAME+ " " + obj.VISIT_DATE + "</a>");
   $('#accordion-1 div ul').append(eventItem);       
});

希望这会有所帮助!

答案 1 :(得分:0)

我找到了答案。

$.each(JSON.parse(jsonVal), function(index, obj){

            jQuery("#id").find('ul').append('<li><font size="2"><a href="visit.html?uname='+uname+'&date='+obj.VISIT_DATE+'">'+obj.LEADNAME+'  '+obj.VISIT_DATE+'" </a></li>');
        });