【问题标题】:How to check if radio button is selected?如何检查单选按钮是否被选中?
【发布时间】:2020-12-04 09:40:18
【问题描述】:

我在回答这个问题时说我对 JavaScript 的了解非常有限,而且我不是开发人员。我被要求帮助为我的公司构建价格计算器向导。

我从 W3 Schools (https://www.w3schools.com/howto/howto_js_form_steps.asp) 找到了一些代码,用于创建一个检查每个输入字段是否为空的表单。我想更改代码以检查是否选择了每个组中的单选按钮。

我不知道如何更改这行代码

if (y[i].value == "")

检查单选按钮是否被选中。

下面是我的一个广播组和检查输入字段是否为空的脚本。

如果您想查看完整的代码,请转到上面的 W3 Schools 链接。

 <div class="tab">Option 1:
    <p><input type="radio" id="male" name="gender" value="male" oninput="this.className = ''"><label for="male">Male</label></p>
    <p><input type="radio" id="female" name="gender" value="female" oninput="this.className = ''"><label for="female">Female</label></p>
  </div>      

function validateForm() {
        // This function deals with validation of the form fields
        var x, y, i, valid = true;
        x = document.getElementsByClassName("tab");
        y = x[currentTab].getElementsByTagName("input");
        // A loop that checks every input field in the current tab:
        for (i = 0; i < y.length; i++) {
          // If a field is empty...
          if (y[i].value == "") {
            // add an "invalid" class to the field:
            y[i].className += " invalid";
            // and set the current valid status to false:
            valid = false;
          }
        }
        // If the valid status is true, mark the step as finished and valid:
        if (valid) {
          document.getElementsByClassName("step")[currentTab].className += " finish";
        }
        return valid; // return the valid status
      }

【问题讨论】:

  • 我不知道你在说什么。我要问的是如何更改代码链接以检查单选按钮是否被选中,而不是检查输入字段是否包含值。
  • 您的问题模棱两可。你想要什么 ??检查所有单选按钮或验证某些值??
  • @tuhin47 你很有帮助

标签: javascript validation radio-button


【解决方案1】:

选中所有单选按钮:

const radioButtons = Array.from(document.querySelectorAll('input[type=radio]'));

function checkChecked(){
  console.log(radioButtons.every(button => button.checked));
}

radioButtons.forEach(button => button.onchange = checkChecked);
<input type=radio />
<input type=radio />
<input type=radio />
<input type=radio />
<input type=radio />

LVL 2:检查所有单选按钮组至少有一个选中:

const groupNames = ['group1', 'group2', 'group3'];
const radioButtons = Array.from(document.querySelectorAll('input[type=radio]'));
    
function checkChecked(){
  console.log(groupNames.every(group =>
    document.querySelector(`input[name=${group}]:checked`)
  ));
}

radioButtons.forEach(button => button.onchange = checkChecked);
<input name="group1" type=radio />
<input name="group1" type=radio />
<input name="group1" type=radio />
<br/>
<input name="group2" type=radio />
<input name="group2" type=radio />
<br/>
<input name="group3" type=radio />
<input name="group3" type=radio />

【讨论】:

  • @mplungjan 我直接回答问题'如何验证是否所有单选按钮都被选中',而不是'如何验证所有单选按钮组都有一些选中按钮'
  • 好的,但这不能回答我的问题。
  • @StevenErickson 我编辑了我的答案,现在这就是你想要的吗?
  • 不,一点也不。我在问如何更改代码行“if (y[i].value == "") {" 以检查单选按钮是否被选中,而不是检查表单字段是否有值。
  • @StevenErickson 不,史蒂夫,你问'如何验证是否所有单选按钮都被选中',我回答的第一个变体给出了这个问题的答案。 y[i].value == "" 需要将单选按钮更改为 if(y[i].checked)
猜你喜欢
  • 2016-01-11
  • 2012-06-18
  • 2014-02-06
  • 2014-04-23
  • 2015-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多