AJAX Request Help for next/previous page

前端 未结 1 1053
野的像风
野的像风 2020-12-06 04:10

How do I use AJAX to get the Next/Previous Page(s).

How to call in a browser:

page.php?page=1-1

or

page.php?page=1
         


        
1条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-06 04:38

    Try something like this... Keep a global variable called currentPage and simply adjust the page number accordingly.

    LIVE DEMO http://jsfiddle.net/Jaybles/MawSB/

    HTML

    
     
    
    Current Page: 1

    JS

    var currentPage=1;
    loadCurrentPage();
    
    $("#next, #prev").click(function(){
        currentPage = 
            ($(this).attr('id')=='next') ? currentPage + 1 : currentPage - 1;
    
        if (currentPage==0) //Check for min
            currentPage=1;
        else if (currentPage==101) //Check for max
            currentPage=100;
        else
            loadCurrentPage();
    });
    
    function loadCurrentPage(){
        $('input').attr('disabled','disabled'); //disable buttons
    
        //show loading image
        $('#displayResults').html(''); 
    
        $.ajax({
            url: '/echo/html/',
            data: 'html=Current Page: ' + currentPage+'&delay=1',
            type: 'POST',
            success: function (data) {
                $('input').attr('disabled',''); //re-enable buttons
                $('#displayResults').html(data); //Update Div
            }
        });
    }
    

    Then your php page can access $_REQUEST['page']; and return data accordingly.

    0 讨论(0)
提交回复
热议问题