使一个复选框充当单选按钮的作用
所以我有这个披萨订单,对于配料部分,我希望能够检查多个选择。例如,用户可以检查意大利辣香肠、香肠、橄榄等。但如果用户选择“无配料”,我希望它清除其余的复选框。我尝试编写一些 JavaScript 来完成此操作,但它不稳定并且只能在单击时起作用。所以基本上,我不希望用户能够选择“无配料”,我不希望选择其他任何内容,并且当用户选择不同的配料时,我不希望选择“无配料”。我希望这是有道理的!任何帮助表示赞赏!到目前为止,这是我的代码:
<script type="text/javascript">
function unCheck(el, n){
el.form.elements[n].checked = false;
}
</script>
<table>
<tr>
<td style="border: none;">
<input name="size" type="radio" value="8.00" id="small"
onclick="this.form.total.value=calculateTotal(this);" />
<label for="small">Small ($8.00)</label>
</td>
<td style="border: none;">
<input type="checkbox" name="topping" id="pepperoni" value="0.50"
onclick="this.form.total.value=calculateTotal(this);"/>
<label for="pepperoni">Pepperoni</label>
<input type="checkbox" name="topping" id="sausage" value="0.50"
onclick="this.form.total.value=calculateTotal(this);" />
<label for="sausage">Sausage</label>
</td>
</tr>
<tr>
<td style="border: none;">
<input name="size" type="radio" value="10.00" id="medium"
onclick="this.form.total.value=calculateTotal(this);" />
<label for="medium">Medium ($10.00)</label>
</td>
<td style="border: none;">
<input type="checkbox" name="topping" id="mushroom" value="0.50"
onclick="this.form.total.value=calculateTotal(this);" />
<label for="mushroom">Mushroom</label>
<input type="checkbox" name="topping" id="olive" value="0.50"
onclick="this.form.total.value=calculateTotal(this);" />
<label for="olive">Olive</label>
</td>
</tr>
<tr>
<td style="border: none;">
<input name="size" type="radio" value="12.00" id="large"
onclick="this.form.total.value=calculateTotal(this);"/>
<label for="large">Large ($12.00)</label>
</td>
<td style="border: none;">
<input type="checkbox" name="Un_CheckAll" value="0.00" id="none"
onclick="unCheck(this, 'pepperoni','sausage', 'mushroom', 'olive'); "/>
<label for="none">No Toppings (Cheese Pizza)</label>
</td>
</tr>
<table>
So I have this pizza order form, and for the topping section, I want multiple selections to be able to be checked. For example, a user can check pepperoni, sausage, olives, etc. But if the user selects "No Toppings", I want it to clear the rest of the checkboxes. I tried writing some JavaScript to do it, but it was unstable and it only worked onClick. So basically, I don't want users to be able to select "No Toppings", I want nothing else to be selected, and when users select a different topping, I don't want "No Toppings" to be selected. I hope this makes sense! Any help is appreciated! Here is my code so far:
<script type="text/javascript">
function unCheck(el, n){
el.form.elements[n].checked = false;
}
</script>
<table>
<tr>
<td style="border: none;">
<input name="size" type="radio" value="8.00" id="small"
onclick="this.form.total.value=calculateTotal(this);" />
<label for="small">Small ($8.00)</label>
</td>
<td style="border: none;">
<input type="checkbox" name="topping" id="pepperoni" value="0.50"
onclick="this.form.total.value=calculateTotal(this);"/>
<label for="pepperoni">Pepperoni</label>
<input type="checkbox" name="topping" id="sausage" value="0.50"
onclick="this.form.total.value=calculateTotal(this);" />
<label for="sausage">Sausage</label>
</td>
</tr>
<tr>
<td style="border: none;">
<input name="size" type="radio" value="10.00" id="medium"
onclick="this.form.total.value=calculateTotal(this);" />
<label for="medium">Medium ($10.00)</label>
</td>
<td style="border: none;">
<input type="checkbox" name="topping" id="mushroom" value="0.50"
onclick="this.form.total.value=calculateTotal(this);" />
<label for="mushroom">Mushroom</label>
<input type="checkbox" name="topping" id="olive" value="0.50"
onclick="this.form.total.value=calculateTotal(this);" />
<label for="olive">Olive</label>
</td>
</tr>
<tr>
<td style="border: none;">
<input name="size" type="radio" value="12.00" id="large"
onclick="this.form.total.value=calculateTotal(this);"/>
<label for="large">Large ($12.00)</label>
</td>
<td style="border: none;">
<input type="checkbox" name="Un_CheckAll" value="0.00" id="none"
onclick="unCheck(this, 'pepperoni','sausage', 'mushroom', 'olive'); "/>
<label for="none">No Toppings (Cheese Pizza)</label>
</td>
</tr>
<table>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 jQuery,这变得微不足道。 查看此示例。
Utilizing jQuery, this becomes trivial. Check out this sample.