Home  >  Q&A  >  body text

jqery ajax issue - Stack Overflow

My question is this

When you click the "Continue Adding" link after 5 items are displayed, click once to add the specified amount of data until the data in the json file is fetched
That is, after 5 items are displayed, click for the first time , displays 6, 7, 8, the second time displays 9, 10, 11, and so on. Is there any way to solve it? My code is as follows:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <script src="http://g.tbcdn.cn/mtb/lib-flexible/0.3.4/??flexible_css.js,flexible.js"></script>
    <title>测试</title>
    <script src="../web/js/jquery-3.2.1.min.js"></script>
    <script src="text.json"type="application/json"></script>
</head>
<body>
<p id="btn">点击</p>
<p id="json">

</p>
<a href="javascript:void(0)" class="clicks" style="display: none">继续添加</a>
<p class="num"></p>
<p class="ccc"></p>
<script>
    $(document).ready(function () {

        $('#btn').click(function () {
            var json=$('#json');
            var num=0;
            var p=$('p');
            json.append('<p class="json"></p>')
            $.ajax({
                type:'post',
                url:'text.json',
                data:null,
                dataType:'json',
                success:function (ress) {
                    var str;
                    str='';
                    $.each(ress,function (index,res) {
                        if(num<5){
                            str+='<p class="lens"><span>名称:'+res['tip']+'</span></br>';
                            str+='<span>作者:'+res['author']+'</span></br></p>';

                            $('.clicks').css({
                                display:'block'
                            })
                            $('.num').html('已加载了'+(num+1)+'条数据....');
                        }

                        json.html(str);
                        num++;



                    });
                    var count=0;
                    $.each(ress,function (index,res) {
                        if(index>4){
                            $('.clicks:last').click(function () {
                                if(count<3){
                                    str= '<p class="lens"><span>'+res['tip']+'</span></br>' +
                                        '<span>'+res['author']+'</span></br></p>'
                                    $('#json').append(
                                        str
                                    )
                                }
                                count++
                            });
                        }
                    })

                    var str1;
                    str1=ress.length-$('.lens').length;
                    $('.ccc').html('还有'+(str1-1)+'条数据');

                },
                error:function (res) {
                    console.log(res)
                }
            })

        })
    })
</script>
<style>
    .lens{border-bottom: 2px solid red}
</style>
</body>
</html>
phpcn_u1582phpcn_u15822598 days ago611

reply all(1)I'll reply

  • 我想大声告诉你

    我想大声告诉你2017-07-05 10:50:17

    For the first request, store the returned data in an array. You can just operate this array the next time you click. There is no need to request again

    reply
    0
  • Cancelreply