douchan0523 2013-08-03 07:28
浏览 126
已采纳

如何在点击事件中使用javascript / jquery逐个获取json中的数据?

I have the working code which fetches all the records from the json, but how can I make the records available one by one on the click of the button(next/previous)?

The following code is working to fetch all the records:

    div
    {
        text-align:center;
        padding:10px;
    }

    #msg {
        width: 500px;
        margin: 0px auto;
    }
    .members {
        width: 500px ;
        background-color: beige;
    }
<input type="button" name="next" id="next"><br/>
<input type="button" name="previous" id="previous"><br/>


<div id="msg">
<table id="userdata" border="1">
    <thead>
        <th>Email</th>
        <th>Sex</th>
        <th>Location</th>
        <th>Picture</th> 
        <th>audio</th> 
        <th>video</th> 
    </thead>
    <tbody></tbody>
</table>
</div>
    $(document).ready(function(){
        var url="json.php";
        $("#userdata tbody").html("");
        $.getJSON(url,function(data){
            $.each(data.members, function(i,user){
                var tblRow =
                    "<tr>"
                    +"<td>"+user.email+"</td>"
                    +"<td>"+user.sex+"</td>"
                    +"<td>"+user.location+"</td>"
                    +"<td>"+"<img src="+user.image+">"+"</td>"
                    +"<td>"+"<audio src="+user.video+" controls>"+"</td>"
                    +"<td>"+"<video src="+user.video+" controls>"+"</td>"
                    +"</tr>" ;
                $(tblRow).appendTo("#userdata tbody");
            });
        });
    });
  • 写回答

2条回答 默认 最新

  • douhao7889 2013-08-03 07:56
    关注

    HTML

    <input type="button" name="next" id="next" value="NEXT" />
    <br/>
    <input type="button" name="previous" id="previous" value="PREV" />
    <br/>
    <div id="msg">
        <table id="userdata" border="1">
            <thead>
                <th>Email</th>
                <th>Sex</th>
                <th>Location</th>
                <th>Picture</th>
                <th>audio</th>
                <th>video</th>
            </thead>
            <tbody></tbody>
        </table>
    </div>
    

    SCRIPT

    var users = [];
    var idx = 0;
    var renderRow = function (idx) {
        var user = users[idx];
        var tblRow = "<tr>" + "<td>" + user.email + "</td>" + "<td>" + user.sex + "</td>" + "<td>" + user.location + "</td>" + "<td>" + "<img src=" + user.image + ">" + "</td>" + "<td>" + "<audio src=" + user.video + " controls>" + "</td>" + "<td>" + "<video src=" + user.video + " controls>" + "</td>" + "</tr>";
        $('#userdata tbody').html(tblRow);
    };
    var url = "json.php";
    $.getJSON(url, function (data) {
        users = data.members;
        renderRow(idx);
        $('#next').click(function() {
            idx++;
            if (idx > (users.length - 1)) idx = (users.length - 1);
            renderRow(idx);
        });
        $('#previous').click(function() {
            idx--;
            if (idx < 0) idx = 0;
            renderRow(idx);
        });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型