1

我正在尝试循环 ajax 调用成功后返回的数据并将结果添加到分页插件。

这是我的代码:

 var imagesPerPage = 2, pageNumber = 1;
  var pagesContainer = $('#pagesContainer'),
  imagesInPage = 0,
  divPage = $("#p1");

    $.ajax({
           type: "POST",
           url: "Default.aspx/GetImages",
           data:{},
           contentType: "application/json; charset=utf-8",
           dataType: "json",
           success: function (response) {

             //Here goes my for loop                    

           }
      });

这是我的 for 循环,我需要从 webmethod 获取图像并将它们循环并添加到分页中。

 for (i = 0; i < response.d.length; i++) {
          if (imagesInPage >= imagesPerPage) {
          imagesInPage = 1;
          pageNumber += 1;
          divPage = $('<div/>', { id: "p" + pageNumber }).addClass('pagedemo').hide().appendTo(pagesContainer);
          } else {
          imagesInPage += 1;
          }
}

这是我的分页默认配置:

 $("#pagination").paginate({
              count: pageNumber,
              start: 1,
              display: Math.min(7, pageNumber),
              border: true,
              border_color: '#fff',
              text_color: '#fff',
              background_color: 'black',
              border_hover_color: '#ccc',
              text_hover_color: '#000',
              background_hover_color: '#fff',
              images: false,
              mouse: 'press',
              onChange: function (page) {
              $('#paginationdemo ._current').removeClass('_current').hide();
              $('#p' + page).addClass('_current').show();
        }
  }); //pagination

现在我的问题是,当我尝试获取一个图像的响应时,它没有向我显示图像和分页。如果它不止一个,那么它开始向我显示分页和图像。

截图

在此处输入图像描述

所以谁能说我哪里错了?

4

2 回答 2

0

where are you trying to get the response for one image? If you are trying to do this in your if condition then it will not work for only one response because

imagesInPage >= imagesPerPage

evaluates to false as imagesPerPage = 2 and imagesInPage=0;

also if your response type is json and this does not solve your problem try using

$.each(response,function(index,data){
    //check each image here 
      // data.id gives you the id of each json object in the response
})
于 2012-04-26T09:43:22.570 回答
0

This is your pagination problem recently i met with the same problem. To fix this you will need a condition. check the length of response. if it is 1 then add some default values and that will work.

于 2012-04-26T09:44:48.887 回答