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条)

报告相同问题?

悬赏问题

  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services