當所有元素'複選框'未選中時,我需要設置一個單選按鈕('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;
}
爲什麼你會爲此使用單選按鈕是/否?使用帶有「訂閱我們的郵件列表」標籤的簡單複選框會更有意義。 – Hubro
單選按鈕是分配所必需的。但你是對的,這確實更有意義。 – David