使一个复选框充当单选按钮的作用

发布于 2024-12-12 01:00:15 字数 2443 浏览 0 评论 0原文

所以我有这个披萨订单,对于配料部分,我希望能够检查多个选择。例如,用户可以检查意大利辣香肠、香肠、橄榄等。但如果用户选择“无配料”,我希望它清除其余的复选框。我尝试编写一些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

命硬 2024-12-19 01:00:15

使用 jQuery,这变得微不足道。 查看此示例

Utilizing jQuery, this becomes trivial. Check out this sample.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文