当所有元素'复选框'未选中时,我需要设置一个单选按钮('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