AJAX Request Help for next/previous page

前端 未结 1 1048
野的像风
野的像风 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

    <input id="next" type="button" value="Next" />
    <input id="prev" type="button" value="Previous" /> 
    <div id="displayResults" name="displayResults">Current Page: 1</div>
    

    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('<img src="http://blog-well.com/wp-content/uploads/2007/06/indicator-big-2.gif" />'); 
    
        $.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)
提交回复
热议问题