【问题标题】:All radio buttons are selected?所有单选按钮都被选中?
【发布时间】:2019-10-25 21:39:54
【问题描述】:

我的 jQuery 代码和单选按钮有问题, 如果我选择了一个按钮,另一个他保持选中状态,我不知道如何解决这个问题,如果有人可以帮助我解决我的问题,谢谢。我留下我的代码和屏幕截图,看看实际发生了什么。

$("#custom").click(function() {
  $(".custom").css({
    "display": "block"
  })
})

$("#man").click(function() {
  $(".custom").css({
    "display": "none"
  })
})

$("#ladies").click(function() {
  $(".custom").css({
    "display": "none"
  })
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="radio" id="ladies" name="ladies" value="ladies">
<label for="ladies">Ladies</label>

<input type="radio" id="man" name="man" value="man">
<label for="man">Man</label>

<input type="radio" id="custom" name="custom" value="custom">
<label for="custom">Custom</label>
<a href="#" class="icons"><i class="fas fa-question-circle fa-1x"></i></a>

<div class="custom">

  <form action="#">
    <select name='gender' id='gender'>
      <option value='gender' disabled>Select the pronoun you are using</option>
      <option value='she'>She: "Wihs her a happy brithday"</option>
      <option value='he'>He: "Wish him a happy birthday!"</option>
      <option value='he/she'>He / She: "Wish him / her a happy birthday!"</option>
    </select>
    <p class="genderTxt">The chosen pronoun is visible to everyone.</p>
    <input type="text" class="optionalG" placeholder="Gender (optional)">
  </form>
</div>

我没有任何错误信息要显示。

【问题讨论】:

  • 一个组中的所有单选按钮必须具有相同的名称。
  • 啊,是的。我是女士。

标签: javascript jquery html jquery-ui


【解决方案1】:

单选按钮组上的名称属性必须具有相同的名称。

改变这个:

 <input type="radio" id="ladies" name="ladies" value="ladies">
 <label for="ladies">Ladies</label>

 <input type="radio" id="man" name="man" value="man">
 <label for="man">Man</label>

 <input type="radio" id="custom" name="custom" value="custom">
 <label for="custom">Custom</label>

到这里

 <input type="radio" id="ladies" name="radioGroup" value="ladies">
 <label for="ladies">Ladies</label>

 <input type="radio" id="man" name="radioGroup" value="man">
 <label for="man">Man</label>

 <input type="radio" id="custom" name="radioGroup" value="custom">
 <label for="custom">Custom</label>

名称不必是“radioGroup”,但必须相同,这样代码才能分辨出哪些单选按钮组属于一起。

【讨论】:

  • 除了用于表单中的&lt;select&gt;
【解决方案2】:

所有&lt;input type="radio"&gt; 标签都需要相同的name 属性。然后就可以了。

<input type="radio" id="ladies" name="gender" value="ladies" checked>
<label for="ladies">Ladies</label>

<input type="radio" id="man" name="gender" value="man">
<label for="man">Man</label>

<input type="radio" id="custom" name="gender" value="custom">
<label for="custom">Custom</label>

【讨论】:

  • 选项标签只存在于选择标签中,不存在问题所引用的单选组。
  • 对不起,我的意思是输入标签
猜你喜欢
  • 1970-01-01
  • 2019-10-04
  • 1970-01-01
  • 1970-01-01
  • 2023-02-03
  • 2020-03-27
  • 1970-01-01
  • 2020-09-14
相关资源
最近更新 更多