2012-02-15 94 views
0

我有3个文件的index.php,.js文件和a.php文件。我通过js(jquery)文件向a.php发送了ajax请求。我是在HTML响应,(在1 HTML响应)有3个div,但我想一次显示一个div。然后点击下一个div,这是在相同的HTML响应。它基本上是通过服务器端数据进行客户端分页(从1个AJAX获得)。但我无法在索引页面中逐一处理3个div。客户端分页通过服务器端数据(Ajax)

1. 的请求进入 -

<script> 
$(document).ready(function(){ 
showitem(id); 
}); 
</script> 
  1. 简单的查询被处理。

    功能showitem(ID){

    $.ajax({  
        type : "POST", 
        cache : true, 
        //dataType: "json", 
        url  : "a.php", 
        data : { 
           proid:id 
           },     
        success: function(data) { 
    
         $("#match_item_display").html(data);// This is span in index.php 
    
        } 
    }); 
    

    }

3. 在响应(比方说)1得到的HTML的div形式9层的元件(3在这种情况下),我想一次显示3个。

$start=0; $end=6; 
    for($d=0;$d<$len;$d++) 
    { 
     if($end>$count) 
     { 
      $end = $count; 
      match_pro("Closley Match", array_slice($recent_arr, $start, $end),$d); 
     } 
     else 
     { 
      match_pro("Closley Match", array_slice($recent_arr, $start, $end),$d); 
      //break; 
     } 
     $start=$end; $end=$end+6; 
    } 

它的回报3个格作一个...输入反应

  1. 要显示 - 下一个和上一个按钮。

SUMMARY - > 1-ajax请求,一些HTML div作为响应,按顺序显示它们在一个按钮上单击一次。

我正在做客户端分页,因为查询一次不能返回超过9-12个响应。所以客户端脚本应该是最佳的。

+0

我们需要看到的代码是在a.php中 - 但是@rajesh是我认为正确的。返回你想要在你的JSON响应中显示的所有项目,并使用jQuery将它们注入到DOM中。 – halfer 2012-02-15 14:19:52

回答

0

你可以把div放在一个数组中然后返回。 如:

$response = array(0 => 'div1_data', 1 => 'div2_data', 2=>'div3_data'); 
echo json_encode('html' => $response); // returning to ajax call 
在Ajax调用的响应,你可以将它保存到一个JS数组

; 然后用第一个元素更新区域。 在下一个按钮点击更新第二个元素的区域... 希望它会有所帮助。

在JS,你可以这样做:

index = 0; 
res_data = []; 
$.ajax({  
type : "POST", 
cache : true, 
//dataType: "json", 
url  : "a.php", 
data : { 
      proid:id 
      },     
    success: function(data) { 
    $(data.html).each(function(k,v){ 

     res_data.push(v); 

    }); 
    showNext();  
    } 
}); 


function showNext(){ 
    $("#match_item_display").html(res_data[index]); 
    index++; 
} 

function showPrev(){ 
    if(res_data[index-1] !== 'undefined'){ 
     $("#match_item_display").html(res_data[index-1]); 
     index--; 
    } 
} 

可以使页面加载的Ajax调用。 并分配showNext()和showPrev来分别按下next和prev按钮的onclick事件。

+0

它的回应来到ajax成功函数的工作,但我也想通过索引页做下一个和以前的功能。如何保存返回json数组在js?请帮帮我...!!!如果你有一些代码plzz分享它.. – 2012-02-16 07:01:42

+0

编辑帖子。它可能会帮助你 – rajesh 2012-02-16 09:15:48

+0

Thanx Rajesh其作品........非常感谢你... – 2012-02-16 12:54:32

相关问题