使2个特定按钮淡出

时间:2014-07-19 18:11:52

标签: javascript jquery

我在jQuery中为我的软件的朋友请求页面写了一个脚本。 这就是我目前正在摇滚的

    $(document).ready(function(){
    $('.acceptfriend').click(function(){
var self = this;
        $.ajax({
            url : 'inc/ajax.php',
            type : 'POST',
            data : {
                action : 'accept_friend',
                uid : $(this).data('uid')
            },
            dataType : 'JSON',
            success : function(result) {
                if (result.xhr == 'success') {
                    $( self ).fadeOut( 1000 );
                } else if (result.xhr == 'error'){
                    alert('An internal error accoured, try again later.');
            }
  }
    });
});
})
$(document).ready(function(){
    $('.rejectfriend').click(function(){
var self = this;
        $.ajax({
            url : 'inc/ajax.php',
            type : 'POST',
            data : {
                action : 'reject_friend',
                uid : $(this).data('uid')
            },
            dataType : 'JSON',
            success : function(result) {
                if (result.xhr == 'success') {
                    $( self ).fadeOut( 1000 );
                } else if (result.xhr == 'error'){
                    alert('An internal error accoured, try again later.');
            }
  }
    });
});
})

但问题是,如果点击其中一个,我想让接受和忽略按钮淡出,我不能使用他们的类名,因为如果我这样做那么那个页面上的那个类的所有按钮都是淡出。 PS。 我知道代码非常混乱,但现在只是,我会稍后清理它。 的修改 这是我用于上面jQuery的HTML makup:

<div class="panel panel-default">
<div class="panel-body">
<a href="profile.php?id=12" class="tip" data-toggle="tooltip" data-placement="top" data-animation="true" data-original-title="anerikanarmyant"><img style="padding-bottom: 5px;" src="http://minecraft-websites.com/assets/images/default_profile_pic.jpg"width="48" height="48"></a>   anerikanarmyant
<div class="pull-right">
<div class="btn-group">
<button data-uid="12" type="button" class="btn btn-success acceptfriend">Accept</button>
<button data-uid="12" type="button" class="btn btn-danger rejectfriend">Ignore</button>
</div>
</div>
</div>
</div>

并且为每个收到的朋友请求重复此代码。

1 个答案:

答案 0 :(得分:1)

以下是两种方法:

1。 http://jsfiddle.net/xWE3V/

$('.acceptfriend,.rejectfriend').click(function(){
    $(this).parent().fadeOut(500);
});

2。 http://jsfiddle.net/P6U76/

$('.acceptfriend').click(function(){
    $(this).siblings( ".rejectfriend" ).fadeOut(500);
    $(this).fadeOut(500);
});

$('.rejectfriend').click(function(){
    $(this).siblings( ".acceptfriend" ).fadeOut(500);
    $(this).fadeOut(500);
});