jQuery.ajax()的分页问题

时间:2012-05-30 05:31:25

标签: jquery ajax pagination magento-1.6

我正在使用magento作为我的CMS,尝试实现ajax for pagination,

目前我有2页,结构就是这样,

<div class="pager">
        <div class="pages">
          <strong>Page:</strong>
          <ol>
             <li class="current">1</li>
             <li>
                <a href="http://localhost/Bakestore/index.php/tools.html?p=2" class="pageLinks">2</a></li>
             <li class="pager_next_img">
                <a class="next i-next pageLinks" href="http://localhost/Bakestore/index.php/tools.html?p=2" title="Next">
                <img src="http://localhost/Bakestore/skin/frontend/default/Bakestore_Theme/images/pager_arrow_right.gif" alt="Next" class="v-middle">
                </a>
            </li>
          </ol>
    </div>
</div>

我的jQuery ajax代码如下,

$j(".pages li a").each(function(){
    $j(this).removeClass('pageLinks').addClass('pageLinks');                                
});

$j(".pageLinks").click(function(e){
    e.preventDefault();
    var anchorSel =  $j(this).attr('href');
    $j.ajax({
        type: "POST", 
        datatype: "HTML",
        cache: true,
        url: anchorSel,

        success: function(data){
            var testdata = $j(data).find(".category-products");
            $j(".col-main .category-products").replaceWith(testdata);
            },

        complete: function(data){
                        $j(".pages li a").each(function(){
                            $j(this).removeClass('pageLinks').addClass('pageLinks');                            });
                            alert($j(".col-main .category-products .toolbar > .pager").html());
                         }
});

单击Page 2时工作正常,但是,当我点击Page 1时,页面重新加载。现在当我再次点击Page 2时,它运行正常。

我调试了我的jQuery,遗憾的是,事件$j(".pageLinks").click根本没有被解雇(当我点击Page 1时)。

知道我哪里出错了?

1 个答案:

答案 0 :(得分:2)

你的返回html包含分页链接? 如果是这种情况,则需要将.click(function(){更改为.live('click', function(){

单击将现有元素仅绑定到该事件处理程序。除了尚未形成的元素之外,live也是如此。

live确实已被弃用。如果你使用1.7,我不认为你是。你应该把它改成

$(document).on('click', ".pageLinks", function(){

});