【问题标题】:JQuery function does not work anymore after pagination分页后JQuery功能不再起作用
【发布时间】:2011-09-21 13:18:52
【问题描述】:

我正在为我的网站使用 CodeIgniter。在一页上,我展示了不同的车辆。用户可以通过分页浏览所有结果。

我还有一些下拉框,用户可以使用它们来进一步过滤结果。一个下拉框包含品牌的主要类别(Mercedes、DAF、...)

第二个下拉框包含车辆类型。对于这个盒子,我创建了一个 JQuery 函数,它根据在第一个框中选择的品牌更新盒子的内容。当我单击分页链接转到下一页时,这不再起作用。我想这与函数是在就绪部分创建的事实有关。有人可以帮忙吗?

JQuery 代码:

<script type="text/javascript">
function updateTypes(){
    $.ajax({
        type: "POST",
        data: "merk_id=" + $('#selection').val()  + "&voertuig=" + $('#voertuig').val(),
        url: "dropdown",
        success: function(msg){
            if (msg != ''){
                $("#selectionresult").html(msg).show();

            }
        }
    });
}


    $("#selection").change(updateTypes);


    // Update types when entering the second box with the mouse
    $("#selectionresult").mouseenter(updateTypes);

第一个框的 id=selection,第二个框的 id=selectionresult

提前致谢!

【问题讨论】:

标签: jquery ajax codeigniter pagination


【解决方案1】:

尝试使用

$('body').delegate('#selection', 'propertychange change', function(){ 
  updateTypes();
}); 

【讨论】:

    【解决方案2】:

    分页时是否会重新呈现选择项?如果是这样,您需要一种方法来再次绑定 .change().mouseenter() 事件,因为原始对象不再存在。

    看看jquery.delegate()

    【讨论】:

    • 不,我认为分页时选择不会重新呈现。我什至觉得整个页面没有重新渲染,但我不确定。
    【解决方案3】:

    听起来.html() 调用清除了先前绑定的click 事件处理程序。使用.live().delegate() 而不是.click()。改变

    $("#selection").change(updateTypes);
    

    $("#selection").live('change', updateTypes);
    // or
    $('some ancestor of #selection').delegate('#selection', 'change', updateTypes);
    

    这样做会将事件侦听器绑定到 DOM 树的更高层,因此侦听器不会被 $("#selection").html() 调用所清除。

    【讨论】:

    • 我试过'code' $("#selection").live('change', updateTypes);但这不起作用。而且我认为我不能使用委托,因为没有#selection 的祖先。你也许有另一个想法这可能是什么?谢谢!
    • body 永远是#selection 的祖先
    猜你喜欢
    • 2013-01-30
    • 1970-01-01
    • 2014-08-21
    • 2022-01-07
    • 2018-03-12
    • 1970-01-01
    • 2013-06-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多