对于JQuery的Ajax调用后循环

对于JQuery的Ajax调用后循环

本文介绍了对于JQuery的Ajax调用后循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想循环AJAX调用后返回的数据被成功添加结果的

I am trying to loop the data returned after ajax call is success and adding the result to pagination plugin.

下面是我的code:

 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和环路他们获得的图像,并添加到分页。

This is my for loop where I need to get the images from webmethod and loop them and add to the pagination.

 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;
          }
}

下面是我的分页默认配置:

Here is my pagination default configuration:

 $("#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

现在我的问题是,当我试图让一个图像的响应它没有显示我的形象以及在pagination.If它不止一个,那么它开始显示我的分页和形象。

Now my problem is when I try to get the response of one Image it is not showing me the image as well as the pagination.If it is more than one then it starts showing me the pagination and image.

截图

因此​​,谁能说我在哪里,我要去哪里呢?

So can anyone say me where am I going wrong?

推荐答案

这是你的分页问题,​​最近我遇到了同样的问题。为了解决这个问题,你需要一个条件。检查响应的长度。如果它是1,则加一些默认值和,将工作。

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.

这篇关于对于JQuery的Ajax调用后循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 00:09