0

すべての要素「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;
}​
4

3 に答える 3

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);
    }
});​
于 2012-11-04T05:43:07.367 に答える
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/

于 2012-11-04T06:00:12.787 に答える
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 />
于 2012-11-04T05:24:45.177 に答える