0

我可以阻止此代码刷新页面吗?

如您所见,我从普通的 click_event 提交了一个 post 操作,因为 live 不支持提交事件,所以我需要它。

jQuery('.verwijder').live('click', function() { 
    t=$(this);

    t.parents(".paneltbnote").animate({ opacity: 'hide' }, "slow",function(){
        t.parents(".wrappertbnote").remove();
        settbnotecounter();

        $db_tbnoteid = t.parents(1).children('.frmnoteid').val();

        $.post("tbnotesact.php", {
            noteid: $db_tbnoteid,
            actie: "verwijder",
            tijd: timestamp
        }, function(xml) {
            // hier nog iets doen
            berichtentoevoegen(xml);//feedback
        });//einde post methode

    });// einde callback animate methode

    return false;    
});//einde binding click event
4

3 回答 3

2

尝试这个:

将第一行替换为

jQuery('.verwijder').live('click', function(e) { t=$(this);

并替换 return false; 和

e.preventDefault();
于 2009-05-20T12:57:04.213 回答
2

您在 click 事件中返回 false ,但不幸的是,它不会停止提交操作。如果您可以选择使用实时事件侦听器,则始终可以使用 bind() 方法观看提交操作。

jQuery('.verwijder').bind('submit', function() {
    /* everything as usual... */
    return false;
});

当然,如果这不是一个选项,您可能只需要在代码中添加一些逻辑,这些逻辑将取消绑定然后重新绑定所有表单的提交操作以执行您想要的操作。

$(function() {
    bind_submits();
    // Let's pretend you have a link that adds forms to your page...
    $('#addForm').live('click', function() {
        add_form();
    });
});

function bind_submits() {
   // We need to unbind first to make we don't multi-bind 
   jQuery('.verwijder').unbind('submit').bind('submit', function() {
        /* everything as usual... */
        return false;
    });
}

function add_form() {
    /* do some stuff to add a form to your layout... */

    // Now reset all current 'submit' binds and add the new one...
    bind_submits();
}

这是在添加 live() 方法之前必须为所有事件侦听器完成的操作(如果您当然没有使用 livequery 插件)。它的编码更多,更难维护,但目前我知道的其他选择并不多。

快乐的 jQuerying...

于 2009-05-20T18:55:30.713 回答
0

很多时候,如果代码中有错误,javascript 永远不会完成函数,直到“return false;”。导致页面重新加载。暂时取消链接,看看是否有任何错误弹出。

于 2009-05-20T15:08:04.167 回答