【问题标题】:parsed json object only returning last element解析的 json 对象只返回最后一个元素
【发布时间】:2014-10-17 22:26:42
【问题描述】:

遍历 getJSON 结果以在 <li> 中显示 json 对象 - 仅返回最后一个元素 - json 对象具有完整结果?遍历 data[i] 应该给我全部?

<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, user-scalable=no">
    <link href="http://jqmdesigner.appspot.com/gk/lib/jquery.mobile/1.4.2/flatui/jquery.mobile.flatui.css" rel="stylesheet" type="text/css" />
    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script src="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.js"></script>
    <title>get the html from a website</title>
    <script>
        $( document ).ready(function() {
        $.getJSON("tt_json.php", function(data){
            for (var i = 0; i < data.length; i++) {
                $.each(data[i], function(key, val) {
                    //alert(key + ":" + val); 
                    $('#myUL').html("<li>" + key + " : " + val + "</li>");  
                });
            } 
        });

    </script>
</head>    
<body>          
<!-- Page: home  -->
    <div id="home" data-role="page">
        <div data-role="header" data-position="fixed" data-theme="b">
        <h3>GET WEB DATA</h3>
        </div>
        <div role="main1" id="mainone" class="ui-content"> 
            <ul data-role='listview' id='myUL' data-inset='true' class='ui-listview ui-listview-inset ui-corner-all ui-shadow'>
            </ul>
        </div>
        <div role="main" id="mainzero" class="ui-content"> 
        </div>
    </div>           
</body>

【问题讨论】:

    标签: json jquery-mobile getjson


    【解决方案1】:

    您似乎在 javascript 函数的循环中设置了带有 id myUL 的 html 标记。这每次都会覆盖该标签的内部 html。尝试添加 li 元素而不是覆盖。

    【讨论】:

    • $('#myUL').html 更改为 $('#myUL').append 已修复,感谢您为我指明正确的方向。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-30
    相关资源
    最近更新 更多