浏览Ajax响应内部


Browsing inside Ajax response

我是jquery&ajax

我有一个包含jquery的php文件,它在选择日期时从另一个php文件加载内容

    $("#datepicker3").change(function(){
        $.ajax({
                type: "POST",
                url: "ptreport1.php",
                data: 'ptdate=' + $(this).val(),
                success: function(resultt){
                    $('#searchr').html(resultt);
                    $("#searchr").show();
                }
                });
});

我面临的问题是ptreport1.php中的内容包含了指向该php文件浏览范围的链接(分页)。当用户点击这些链接时,他会被引导出加载ajax内容的原始页面。是否有任何方法可以防止这种情况并在原始页面中加载内容。

更新#2。

在用preventDefault编辑代码后,下面是我的jquery

<script>
$(document).ready(function() {    
$("#searchr").hide(); 
$(function() {
$('#datepicker3' ).datepicker({
changeMonth: true,
changeYear: true,
dateFormat: 'dd M yy'
});
});
    $("#datepicker3").change(function(){
    var myValue = $(this).val();
        $.ajax({
                type: "POST",
                url: "ptreport1.php",
                data: 'ptdate=' + $(this).val(),
                success: function(resultt){
                    $('#searchr').html(resultt);
                    console.log('html.resultt');
                    $("#searchr").show();
                $('.pagination-link').click(function(e) {
                        e.preventDefault();
                       //Another AJAX call to load the content here.
                       var page = $("#pagediv").html();
                       var timestamp =$("#timediv").html();
                       var dataString = 'page='+ page + '&timestamp='+ timestamp;
                               $.ajax({
                type: "GET",
                url: "ptreport1.php",
                data: dataString,
                success: function(resultt){
                    $('#searchr').html(resultt);
                    console.log('html.resultt');
                    $("#searchr").show();
                }
                });
                    });    
                }
                });
});  
});
</script>

和ptreport1.php

<?php
/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
include "core.php";
connectdb();
$page = $_GET['page'];
$timestamp = $_GET['timestamp'];
$ptdate = $_POST['ptdate'];
if(isset($timestamp)){
$ptdate = date('d M Y', $_GET['timestamp']);
}else{    
$timestamp =  strtotime($ptdate);  
}
 if(!isset($page)){
     $page=1;
 }
    if($page=="" || $page<=0)$page=1;
$numresults = mysqli_query($connect, "SELECT patient.firstname, patient.lastname, appointment.charge FROM patient INNER JOIN appointment ON  patient.id = appointment.patientid WHERE DATE_FORMAT(appointment.date, '%d %b %Y')='".$ptdate."'")or die("error querying");
$numrows = mysqli_num_rows($numresults);
    $num_items = $numrows; //changable
    $items_per_page= 5;
    $num_pages = ceil($num_items/$items_per_page);
    if($page>$num_pages)$page= $num_pages;
    $limit_start = ($page-1)*$items_per_page;    
    $npage = $page+1;
$numresults = mysqli_query($connect, "SELECT patient.firstname, patient.lastname, appointment.charge FROM patient INNER JOIN appointment ON  patient.id = appointment.patientid WHERE DATE_FORMAT(appointment.date, '%d %b %Y')='".$ptdate."' LIMIT $limit_start, $items_per_page")or die("error querying");
$numrows = mysqli_num_rows($numresults);
if($numrows==0){
    echo "Sorry, No results for $ptdate.";
    exit();
}
echo "<p><table>";
while($msdrow = mysqli_fetch_assoc($numresults)){
        $charge= $msdrow['charge'];
        $fname= $msdrow['firstname'];
        $lname= $msdrow['lastname'];
      // $fullname = $fname." ".$lname;
echo "<tr><td>$fname</td><td>$lname</td><td>$charge</td></tr>";
$totalcharge = $totalcharge+$charge;
    }
echo "<b><tr><td colspan='"2'">Total Charge</td><td>$totalcharge</td></tr></b></table></p>";
echo "<div id='pagediv' >";
echo "$page</div>";
echo "<div id='timediv' >";
echo "$timestamp</div>";  
  echo "<p align='"center'">";   
    if($page>1)
    {
      $ppage = $page-1;
      echo "<a href='"ptreport1.php?timestamp=$timestamp&amp;page=$ppage'" class='"pagination-link'">&#171;PREV</a> ";
    }
    if($page<$num_pages)
    {
      $npage = $page+1;
      echo "<a href='"ptreport1.php?timestamp=$timestamp&amp;page=$npage'" class='"pagination-link'">Next&#187;</a>";
    }
    echo "<br/>$page/$num_pages<br/>";
echo "</p>";

我在这里做错了什么,因为它在第二次点击后跳出ajax,直接指向原始php页面。

Thanx

在ptreport1.php中加载内容后,您需要在分页链接上设置onclick事件处理程序。您需要更改.pagination链接以匹配正在加载的内容中的链接。

success: function(resultt){
                    $('#searchr').html(resultt);
                    $("#searchr").show();
                    $('.pagination-link').click(function(e) {
                        e.preventDefault();
                       //Another AJAX call to load the content here.
                    });
                }