通过从正常的clickevent提交帖子操作来阻止页面刷新

时间:2022-09-26 09:47:28

my question is Can I prevent a page refresh from this code

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

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

正如您所看到的,我从正常的click_event提交了一个帖子操作,因为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

3 个解决方案

#1


try this:

replace the first line with

用第一行替换

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

and replace return false; with

并替换return false;同

e.preventDefault();

#2


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.

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

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();
}

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.

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

Happy jQuerying...

#3


A lot of times, if theres an error in the code, javascript never gets to complete the function up to "return false;" resulting in a page reload. Take off the link for now, and see if any errors pop-up.

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

#1


try this:

replace the first line with

用第一行替换

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

and replace return false; with

并替换return false;同

e.preventDefault();

#2


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.

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

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();
}

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.

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

Happy jQuerying...

#3


A lot of times, if theres an error in the code, javascript never gets to complete the function up to "return false;" resulting in a page reload. Take off the link for now, and see if any errors pop-up.

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