从Jquery Ajax获得上一个,接下来

时间:2015-06-13 11:08:06

标签: php jquery arrays ajax json

我想通过点击next和prev来加载特定div上数据库的内容。 我正在通过下面的示例说明阵列,我不明白如何为Prev和next编码!

对于产品页面,单击下一步,上一页显示相关产品!!

Array
(
    [0] => 7319
    [1] => 38089
    [2] => 34203
    [3] => 35604
    [4] => 36209
    [5] => 34978
    [6] => 35196
    [7] => 35856
    [8] => 39841
    [9] => 23944
    [10] => 38239
    [11] => 34866
) 

HTML

<a class="ajx" name="prev" >Prev</a>
<a class="ajx" name="next" >Next</a>

JS

 $(function () {

        $('.ajx').on('click', function(){

        var rel_cars_array = <?php echo json_encode($rel_cars_ajax); ?> 

        var data = {'sale_id' :<?php echo current($rel_cars_ajax);?>};

            $.ajax({
              type: "GET",
              data: data,
              url: '<?php echo base_url();?>ajaxdetail',
              success: function (response) {
                $("#detail-con").html(response);
              }
            }); 
        });
});

1 个答案:

答案 0 :(得分:0)

我得到了解决方案

JS

  $(function () {     

    $('.ajx').on('click', function(){   
    var rel_cars_array = <?php echo json_encode($rel_cars_ajax); ?> 
    rel_cars_array.unshift(<?php echo $car["salable_registered_id"];?>);

    if(typeof start == 'undefined'){
        start = rel_cars_array[0];
    }

    if($(this).attr('name')=='prev'){
        var prev = rel_cars_array[($.inArray(start, rel_cars_array) - 1 + rel_cars_array.length) % rel_cars_array.length];
        start = prev;   
    }
    if($(this).attr('name')=='next'){
        var next = rel_cars_array[($.inArray(start, rel_cars_array) + 1) % rel_cars_array.length];
        start = next;       
    }

    var data = {'sale_id' : start };

        $.ajax({
          type: "GET",
          data: data,
          url: '<?php echo base_url();?>ajaxdetail',
          success: function (response) {
            $("#detail-con").html(response);
          }
        }); 
    });
});