-1

最近有人给了我这个代码来解决我的单选按钮问题,它工作得很好但是我怎样才能排除更多的单选按钮组?

我还想排除“lunch_fruitjuice”以及“lunch_desserts”

$(function(){
    // cache the selector for all target radio buttons for later use
    var targetRadioButtons = $("input:radio").not("input:radio[name=lunch_desserts]");

    targetRadioButtons.change(function(){
        var currentGroupName = $(this).prop("name");       

        // remove selected state from other items
        targetRadioButtons.not(this).prop('checked', false);
    });
})

谢谢

4

4 回答 4

1
var targetRadioButtons = $("input:radio").not("input:radio[name=lunch_desserts]").not("input:radio[name=lunch_fruitjuice]");
于 2013-10-29T14:55:40.317 回答
1

这可能有点难看,但它应该可以工作:

$("input:radio").not("input:radio[name=lunch_desserts]").not("input:radio[name=lunch_fruitjuice]").not(...)

您所要做的就是not()一个接一个地链接命令,排除您想要的所有元素。

更好的方法是为您确实想要处理类似类的所有无线电输入提供(例如use_these_ones)。这样,该行将像这样简单:

var targetRadioButtons = $("input.use_these_ones")
于 2013-10-29T14:58:12.157 回答
0
var exclude = ['lunch_fruitjuice', 'lunch_desserts'];
var targetRadioButtons = $("input:radio");
for (var i = 0; i < exclude .length; i++)
    targetRadioButtons = targetRadioButtons.not("input:radio[name="+exclude[i]+"]");
于 2013-10-29T14:58:22.953 回答
0

如果您想从您的选择中排除某些单选按钮,您可能应该为您希望排除的单选按钮添加一个类。像这样:

HTML

<div><input type="radio" name="lunch_desserts" class="exclude" value="Jello"/> Jello</div>
<div><input type="radio" name="lunch_desserts" value="Pudding"/> Pudding</div>
<div><input type="radio" name="lunch_desserts" class="exclude" value="Candy"/> Candy</div>
<div><input type="radio" name="lunch_fruitjuice" value="Apple Juice"/> Apple Juice</div>
<div><input type="radio" name="lunch_fruitjuice" class="exclude" value="Orange Juice"/> Orange Juice</div>
<div><input type="radio" name="lunch_fruitjuice" value="Guava Juice"/> Guava Juice</div>

JavaScript

// Select radio buttons that do not have the exclude class
var targetRadioButtons = $("input:radio:not(.exclude)");

// Test it by disabling just the targetRadioButtons
targetRadioButtons.attr("disabled", true);

演示:http: //jsfiddle.net/J2DXF/

于 2013-10-29T14:56:59.123 回答