0

我有 3 组按钮,它们的行为类似于单选按钮。

单选按钮工作正常,但我需要删除 ID 为 #filter1-none 的按钮的活动类,前提是其他任何按钮都具有活动类。最好的方法是 if/then 解决方案还是我可以使用 .filter 来做到这一点?

这是无线电行为。

$('.filter .btn').click(function () {
    $(this).addClass('active').siblings('.active').removeClass('active');
});

这是html:

<div class="btn-group pull-right filter" data-toggle="buttons-radio">
    <button type="button" class="btn btn-large btn-flat" id="filter1-none">Show All</button>
</div>

<div class="btn-group pull-right mr filter" data-toggle="buttons-radio">
    <button type="button" class="btn btn-large btn-flat" id="filter1-LI">LI</button>
    <button type="button" class="btn btn-large btn-flat" id="filter1-LPO">LPO</button>
    <button type="button" class="btn btn-large btn-flat" id="filter1-NLI">NLI</button>
</div>

<div class="btn-group pull-right mr filter" data-toggle="buttons-radio">
    <button type="button" class="btn btn-large btn-flat" id="filter1-Low">Low</button>
    <button type="button" class="btn btn-large btn-flat" id="filter1-Medium">Medium</button>
    <button type="button" class="btn btn-large btn-flat" id="filter1-Urgent">Urgent</button>
</div>
4

3 回答 3

1
  $('.filter .btn').click(function () {
        $('#filter1-none').removeClass('active');
        $(this).addClass('active').siblings('.active').removeClass('active');
    });
于 2012-11-14T23:42:34.567 回答
0

我不确定我是否理解正确,但要知道是否有任何其他元素具有“活动”类,为什么不检查选择器返回的数组是否有任何元素?

if ($('.active').length > 1) {
    // There are two elements with the 'active'
    // class. One of them is the button with id
    // filter1-none
    $('#filter1-none').removeClass('active');
}
于 2012-11-14T23:22:23.600 回答
0

如果我理解正确,这就是你想要的 javascript。如果您单击的按钮未#filter1-none从中删除活动类

var $none = $('#filter1-none');
$('.filter .btn').click(function () {
   $none.removeClass('active');
   $(this).addClass('active').siblings('.active').removeClass('active');
});​
于 2012-11-14T23:43:18.903 回答