通过从正常clickevent提交后操作防止页面刷新

通过从正常clickevent提交后操作防止页面刷新

本文介绍了通过从正常clickevent提交后操作防止页面刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是
我可以阻止从这个代码页面刷新

my question isCan I prevent a page refresh from this code

正如你可以看到,我提交一个正常的click_event我需要
,因为live不支持提交事件。

As you can see, I submit a post action from a normal click_event, wich I needbecause live does not support submit events.

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


推荐答案

您在点击事件中返回false,但它不会停止提交操作,不幸的是。如果您有使用直播活动监听器的选项 ,您可以随时使用bind()方法观看提交操作。

You are returning false in the click event but it's not going to stop the submit action, unfortunately. If you have the option of not using the live event listener, you can always just watch the submit action with the bind() method.

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

当然,如果这不是一个选项,你可能只需要添加一些逻辑到你的代码将解除绑定,然后重新绑定所有表单的提交操作,以做你想要的。

Of course, if that's not an option, you might just have to add some logic into your code that will unbind and then rebind all forms' submit actions to do what you want.

$(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插件当然)。它更多的编码和更难维护,但目前还没有真正太多的其他选项,我知道。

This is what had to be done for all event listeners before the live() method was added (if you didn't use the livequery plugin of course). It's more coding and harder to maintain but there aren't really too many other options at the moment that I'm aware of.

快乐jQuerying ...

Happy jQuerying...

这篇关于通过从正常clickevent提交后操作防止页面刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 21:02