简体   繁体   English

如何在表单提交上检查单选按钮 - JavaScript

[英]How to check radio button is checked on form submit - JavaScript

I have a form using radio buttons for the user to select a rating between 1-10.我有一个使用单选按钮的表单,供用户选择 1-10 之间的评级。 Some of these questions are required to have a rating before the user is able to submit.其中一些问题需要在用户能够提交之前获得评级。 So i call for the function validateForm() on submit to do so.所以我在提交时调用函数 validateForm() 来这样做。

Example of radio buttons:单选按钮示例:

          <div class="fluid_container">
    <label for="ease-rating-0">
<input type="radio" name="ease-rating" class="hover-select-ease" id="ease-rating-0" value="0" />
<div class="box-number-half number-padding">0</div>
    </label>

      <label for="ease-rating-1">
<input type="radio" name="ease-rating" class="hover-select-ease" id="ease-rating-1" value="1" />
<div class="box-number-half number-padding">1</div>
</label>

      <label for="ease-rating-2">
 <input type="radio" name="ease-rating" class="hover-select-ease" id="ease-rating-2" value="2" />
<div class="box-number-half number-padding">2</div>
</label>

      <label for="ease-rating-3">
<input type="radio" name="ease-rating" class="hover-select-ease" id="ease-rating-3" value="3" />
<div class="box-number-half number-padding">3</div>
</label>

      <label for="ease-rating-4">
<input type="radio" name="ease-rating" class="hover-select-ease" id="ease-rating-4" value="4" />
<div class="box-number-half number-padding">4</div>
</label>  
          
          
    <label for="ease-rating-5">
<input type="radio" name="ease-rating" class="hover-select-ease" id="ease-rating-5" value="5" />
<div class="box-number-half number-padding">5</div>
</label>

      <label for="ease-rating-6">
<input type="radio" name="ease-rating" class="hover-select-ease" id="ease-rating-6" value="6" />
<div class="box-number-half number-padding">6</div>
</label>

      <label for="ease-rating-7">
 <input type="radio" name="ease-rating" class="hover-select-ease" id="ease-rating-7" value="7" />
<div class="box-number-half number-padding">7</div>
            </label>

      <label for="ease-rating-8">
<input type="radio" name="ease-rating" class="hover-select-ease" id="ease-rating-8" value="8" />
<div class="box-number-half number-padding">8</div>
            </label>

      <label for="ease-rating-9">
<input type="radio" name="ease-rating" class="hover-select-ease" id="ease-rating-9" value="9" />
<div class="box-number-half number-padding">9</div>
</label>
          
         
      <label for="ease-rating-10">
<input type="radio" name="ease-rating" class="hover-select-ease" id="ease-rating-10" value="10" />
<div class="box-number-half">10</div>
</label>
      </div>

An example of my form: Visual Example我的表单示例: Visual Example

I have success with the following code for "ease of placing order" question:对于“易于下订单”问题,我使用以下代码取得了成功:

function validateForm() {
    var easeradios = document.getElementsByName("ease-rating");
    
    for (var i = 0; i < easeradios.length; i++) {
        if (easeradios[i].checked) {
        return true;
        }
    } return false;
}

I am grabbing the radio buttons by their name which for this is ease-rating.我正在按它们的名称抓取单选按钮,这就是轻松评级。 This works fine and the user cannot submit until a rating is selected on this question.这工作正常,用户无法提交,直到在此问题上选择评级。 However when i try to apply the same to the next row as this is also required it seems to go wrong.但是,当我尝试将其应用于下一行时,因为这也是必需的,它似乎出错了。

    function validateForm() {


var easeradios = document.getElementsByName("ease-rating");

for (var i = 0; i < easeradios.length; i++) {
    if (easeradios[i].checked) {
    return true;
    }
} return false;


var convradios = document.getElementsByName("conv-rating");

for (var i = 0; i < convradios.length; i++) {
    if (convradios[i].checked) {
    return true;
    }
} return false;
    
   
}

It seems to only acknowledge a requirement for "Convenience of delivery" before submitting instead of taking the first questions requirement now.它似乎只在提交之前承认“交付便利”的要求,而不是现在接受第一个问题的要求。

Sorry i'm new to this but what i want is for the form to check that a rating is selected on each question before proceeding.抱歉,我是新手,但我想要的是表格在继续之前检查每个问题是否选择了评级。 I don't want to use /required as i want to insert custom error message underneath the rating required which is why i haven't used that.我不想使用 /required,因为我想在所需的评级下方插入自定义错误消息,这就是我没有使用它的原因。

Thanks for your help!谢谢你的帮助!

Your are returning from the for loops too early, which is why the rest of the code where conradios for loop resides is unreachable.您过早地从for循环返回,这就是为什么conradios for循环所在的其余代码无法访问的原因。

Correct this by place the return false;通过放置return false; statement just before the last curly brace of the for loop, not after it.语句就在for循环的最后一个花括号之前,而不是在它之后。

for (var i = 0; i < easeradios.length; i++) {
    if (easeradios[i].checked) {
      return true;
    }
    return false;
} 

Do the same for the second loop as well:对第二个循环也执行相同的操作:

for (var i = 0; i < convradios.length; i++) {
    if (convradios[i].checked) {
      return true;
    }
    return false;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM