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

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

正如您所看到的,我从正常的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 

试试这个:

用第一行替换

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

并替换return false; 同

 e.preventDefault(); 

您在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 …

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