3

html:

<div style="width: 260px;margin:25px 0 0 30px">
<input type="checkbox"  name="send_email" class="delete_followup" />Send email alerts
<input type="checkbox" value="delete" type="checkbox" />Send SMS alerts <button type="submit" name="delete" value="{{follower.id}}" class="delete_follower">Delete</button>
</div>

js:

$(".delete_followup").click(function(){
var $this = $(this);
$(this).find(".delete_follower").show();           
});

我想在单击delete_followupclass.i 时显示隐藏按钮。我用上面的 jQuery 尝试但没有工作。

4

4 回答 4

1

delete_follower元素不是元素的后代delete_followup,它是兄弟元素,因此find()您需要使用siblings()

$(".delete_followup").click(function(){
    var $this = $(this);
    $this.siblings(".delete_follower").show();           
});
于 2013-09-03T16:47:18.213 回答
1

或尝试.nextAll

$(this).nextAll(".delete_follower").show();

在这里工作:http: //jsfiddle.net/tw5XK/

于 2013-09-03T16:49:18.413 回答
1

试试这个:

$(".delete_followup").click(function () {
    if (this.checked) {
        $(this).siblings(".delete_follower").show();
    } else {
        $(this).siblings(".delete_follower").hide();
    }
});

演示在这里

于 2013-09-03T16:50:33.460 回答
1

当您已经拥有对所需元素的引用时,您正试图向下搜索 div。让它变得比它需要的更复杂,哈哈

$(".delete_followup").click(function(){
   $(this).show();           
});

每当您触发单击事件时,单击的实际元素都会作为函数的范围传递。由于您触发了“.delete_followup”的单击,因此该 div 是您的元素范围

于 2013-09-03T17:00:10.473 回答