当所有复选框都为false时,选中单选按钮

时间:2012-11-04 04:26:25

标签: javascript html forms radio-button checkbox

当取消选中所有元素'复选框'时,我需要将单选按钮('no_subscribe')设置为true。取消选中所有这些复选框后,应该会发生这种情况。 如果在其他地方被问过,请原谅我。我是javascript和编程的新手,所以任何帮助都非常感谢!我已经能够使'订阅'按钮做我想要的,但我无法弄清楚相反的情况。这些都在一个名为“邮件程序”的表单中。我最近删除了我的尝试。他们没有工作,所以很抱歉没有代码可以排除故障。

<fieldset>
Subscribe to our Mailing List?
        <input type = "radio" name = "yes" id = "subscribe" onclick = "subscribe_to_all();" value = "yes" />Yes
        <input type = "radio" name = "no" id = "no_subscribe" onclick = "subscribe_to_none();" value = "no" />No
</fieldset>

<fieldset name = "subscribe">
<legend>Mailing List Subscriptions</legend>
    <input type="checkbox" id = "c1" name="subscriptions" onclick = "radio_yes();"  value="CorporateEmails"/>
    Corporate Press Release Emails<br />
    <input type="checkbox" id = "c2" name="subscriptions" onclick = "radio_yes();"  value="SoftwareAdvertising"/>
    Upgrade Software Advertising List<br />
    <input type="checkbox" id = "c3" name="subscriptions" onclick = "radio_yes();"  value="PostBuyoutSpammers"/>
    List given to spammers after buyout<br />
    <input type="checkbox" id = "c4" name="subscriptions" onclick = "radio_yes();"  value="NonCynical"/>
    The only non-cynical list<br />
    <input type="checkbox" id = "c5" name="subscriptions" onclick = "radio_yes();"  value="SelltoSpammers"/>
    List to sell to spammers<br />
    <input type="checkbox" id = "c6" name="subscriptions" onclick = "radio_yes();"  value="SpamList"/>
    Spam List<br />

外部javascript文件:

function subscribe_to_all() {
    for (i = 0; i < document.mailer.subscriptions.length; i++) {
        document.mailer.subscriptions[i].checked = true;
    }
}

function subscribe_to_none() {
    for (i = 0; i < document.mailer.subscriptions.length; i++) {
        document.mailer.subscriptions[i].checked = false;
    }
}

function radio_yes() {
    document.getElementById("subscribe").checked = true;
}​

3 个答案:

答案 0 :(得分:1)

通过使用jQuery,您可以通过以下方式解决此问题:

class="subscriptions"属性添加到所有复选框。

$('.subscriptions').change(function() {
    var allUncheck = true;
    $('.subscriptions').each(function() {
        if ($(this).is(':checked')) allUncheck = false;
    });
    if (allUncheck == true) {
        $('#no_subscribe').attr('checked', 'true');
    } else {
        $('#no_subscribe').attr('checked', false);
    }
});​

答案 1 :(得分:0)

我使用了一个新功能。它没有优化。我用过一个新功能。在那里,您可以在循环中获取复选框状态。但我的代码有效。只需复制并粘贴并查看更改

即可
    <script type="text/javascript">
function subscribe_to_all() {

for (i=0;i<document.mailer.subscriptions.length;i++){

document.mailer.subscriptions[i].checked = true;

}
}

function subscribe_to_none() {

for (i=0;i<document.mailer.subscriptions.length;i++){

document.mailer.subscriptions[i].checked = false;

}
}
function radio_yes() {

document.getElementById("subscribe").checked = true;

}
function radio_uncheck()
{


        c1=document.getElementById("c1").checked;
        c2=document.getElementById("c2").checked;
        c3=document.getElementById("c3").checked;
        c4=document.getElementById("c4").checked;
        c5=document.getElementById("c5").checked;
        c6=document.getElementById("c6").checked;
        console.log(c1);
        if(c1==false && c2==false && c3==false && c4==false && c5==false && c6==false)
        {document.getElementById("subscribe").checked=false;}

    //document.getElementById("subscribe").checked=false;
} 
</script>

<fieldset>
Subscribe to our Mailing List?
        <input type = "radio" name = "yes" id = "subscribe" onclick = "subscribe_to_all();" value = "yes" />Yes
        <input type = "radio" name = "no" id = "no_subscribe" onclick = "subscribe_to_none();" value = "no" />No
</fieldset>

<fieldset name = "subscribe">
<legend>Mailing List Subscriptions</legend>
    <input type="checkbox" id = "c1" name="subscriptions" onclick = "radio_yes();radio_uncheck();"  value="CorporateEmails"/>
    Corporate Press Release Emails<br />
    <input type="checkbox" id = "c2" name="subscriptions" onclick = "radio_yes();radio_uncheck();"  value="SoftwareAdvertising"/>
    Upgrade Software Advertising List<br />
    <input type="checkbox" id = "c3" name="subscriptions" onclick = "radio_yes();radio_uncheck();"  value="PostBuyoutSpammers"/>
    List given to spammers after buyout<br />
    <input type="checkbox" id = "c4" name="subscriptions" onclick = "radio_yes();radio_uncheck();"  value="NonCynical"/>
    The only non-cynical list<br />
    <input type="checkbox" id = "c5" name="subscriptions" onclick = "radio_yes();radio_uncheck();"  value="SelltoSpammers"/>
    List to sell to spammers<br />
    <input type="checkbox" id = "c6" name="subscriptions" onclick = "radio_yes();radio_uncheck();"  value="SpamList"/>
    Spam List<br />

答案 2 :(得分:0)

这可以使用jquery来解决。第一个字段集中的单选按钮应该具有相同的名称值...我已经在您的代码上尝试了这一点。看看吧。我为字段集添加了一个id“搜索”...

$(function() {
 $('#search input:checkbox').click(function() {
  var classes = $('#search input:checkbox:checked');  
  if(classes.length == 0)
    {
        $('input:radio[name=no]:nth(1)').attr('checked',true);
    }  
  else
    {
       $('input:radio[name=no]:nth(1)').attr('checked',false);
    }             
});
});

这是工作小提琴。 http://jsfiddle.net/NBCX8/4/