English 中文(简体)
防止 Default 不与 jQuery. live 一起工作
原标题:preventDefault does not work with jQuery.live

我有一张联署材料:

$( .self_delete ).live( click , function(e){
    e.preventDefault();
    $.ajax({
        type:  DELETE ,
        url: $(this).attr( href ),
        success:  $(this).parent().remove(),
        dataType: "script"
    })
});

瞄准此 HTML 目标 :

<a href="/list_items/34" class="self_delete" data-method="delete" rel="nofollow">
    <i class="icon-trash"></i>
</a>

问题是当我点击链接时, 它会首先向服务器提交ajax调用, 然后发送普通的 HTML 调用 。 当然, 自从列表项目已经删除了, 这样的事情就会混乱起来 。

我有一种感觉,这是由现场电话引起的,但我不明白为什么。我的JS-Fu需要一些训练!


EDIT changed the code to this:

$( .self_delete ).live( click , function(e){
    e.preventDefault();
    var self = $(this);
    $.ajax({
        type:  DELETE ,
        url: self.attr( href ),
        success:  function(){ self.parent().remove() },
        dataType: "script"
    })
});

我仍然有同样的问题。试图用.on替换.life, 但它甚至没有试图通过AJAX提交,所以我想我必须更仔细地阅读医生:-S。

最佳回答

您需要将成功代码包裹在一个匿名函数中, 当它立即运行时, 您需要将成功代码包在一个匿名函数中, 同时在另一个变量中存储引用 $(this) , 如 self

$( .self_delete ).live( click , function(e){
e.preventDefault();
var self = $(this); 
$.ajax({
    type:  DELETE ,
    url: self.attr( href ),
    success:  function(){ self.parent().remove() }, 
})
});

旁注,您应该使用 .on ,直到 jQuery 1. 7 . live 已经失效。

$(document).on( click ,  .self_delete , function(e){
    e.preventDefault();
    var self = $(this); 
    $.ajax({
        type:  DELETE ,
        url: self.attr( href ),
        success:  function(){ self.parent().remove() }, 
    })
});
问题回答

暂无回答




相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...

热门标签