2015-04-02 60 views
1

我試圖隱藏li,當我點擊裏面的button如何隱藏上級元素?

不工作。

<li class="list-group-item jogador"> 
<button class="btn btn-danger excluir">Excluir</button> 
</li> 


<script> 
$('.excluir').click(function(){ 
$.ajax({ 
    url: '/test/', 
    method: 'post', 
    success: function() { 
    $('.jogador').closest().hide(); 
    } 
}); 
}); 
</script> 

使用$(this).prev().hide();進行測試,但它也不起作用。

回答

2

你要選擇父:

$('.excluir').click(function(){ 
var $t = $(this);//so that we can use this after the callback 
$.ajax({ 
    url: '/test/', 
    method: 'post', 
    success: function() { 
    $t.parent().hide();//select our parent 
    } 
}); 
}); 
2

.jogador是LI!

$('.excluir').click(function(){ 

var $theButton = $(this);    // Reference the clicked button 

$.ajax({ 
    url: '/test/', 
    method: 'post', 
    success: function() { 
     $theButton.closest("li").hide(); // and hide it's closest LI element 
    } 
}); 

});