如果选择了单选按钮,则检查 JavaScript 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5223859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 16:26:04  来源:igfitidea点击:

JavaScript function checked if a radio button is selected

javascriptfunctionradio-button

提问by Narek

<label for="Merital Status">Marital Status:</label>
    <input type="radio" title="Marital Status" name="Marital_Status" id="Marital Status" value="Single"/>Single
    <input type="radio" title="Marital Status" name="Marital_Status" value="Married"/>Married
    <input type="radio" title="Marital Status" name="Marital_Status" value="Divorced"/>Divorced

And I want to write a JavaScript function that checks whether a radi button named "Merital_Status" is selected. I represent the function that I wrote for this purpose. The function gets as an argument the element id and returnes boolen:

我想编写一个 JavaScript 函数来检查是否选择了名为“Merital_Status”的单选按钮。我代表我为此目的编写的函数。该函数获取元素 id 作为参数并返回 boolen:

function radio_button_checker(elemId)
{
    var radios = document.getElementsByTagName(elemId);

    var value = false;


    for (var i = 0; i < radios.length; i++) 
    {

        if (radios[i].checked) 
        {

            value = true;
            break;
        }
    }
    return value;
}

And I invoke this function like this:

我像这样调用这个函数:

if (radio_button_checker('Marital_Status') == false) 
{
    alert('Please fill in your Merital Status!');
    return false;
}

But it does not work. Please tell me how to modify my function in order to check if radiobutton is checked.

但它不起作用。请告诉我如何修改我的函数以检查是否选中了单选按钮。

回答by fabsn

What you are doing is looking for an element with the tag name "Merital_Status". Replace document.getElementsByTagNamewith document.getElementsByNameand it should work.

您正在做的是寻找具有标记名称“Merital_Status”的元素。替换document.getElementsByTagNamedocument.getElementsByName它应该可以工作。

回答by scunliffe

You are mixing ID's and NAME's.

您正在混合ID's 和NAME's。

Your radio button "set" needs to all have the same name (which you have), and if you needto refer to them individually by id, then you'll need to add an id to the last two (currently not set... and not required if you apply the labels to each individual option). You will want the label tags for each radio button as it improves the usability by allowing the user to click on the word not just the small radio button.

您的单选按钮“设置”需要全部具有相同的名称(您拥有的名称),并且如果您需要通过 id 单独引用它们,那么您需要在最后两个中添加一个 id(当前未设置 .. . 如果您将标签应用于每个单独的选项,则不需要)。您将需要每个单选按钮的标签标签,因为它通过允许用户单击单词而不仅仅是小单选按钮来提高可用性。

Marital Status:
  <label><input type="radio" name="Marital_Status" value="Single"/>Single</label>
  <label><input type="radio" name="Marital_Status" value="Married"/>Married</label>
  <label><input type="radio" name="Marital_Status" value="Divorced"/>Divorced</label>

However when you test... you want to see that at least 1 radio in the set is checked. You can do this with:

但是,当您测试时...您希望看到该集合中至少有 1 个收音机被选中。你可以这样做:

function radioButtonChecker(fieldNAME){
  var radioSet = document.forms[0].elements[fieldName];
  for(var i=0;i<radioSet.length;i++){
    if(radioSet[i].checked){
      return true;
    }
  }
  return false;
}

There are a few presumptions made here.

这里有一些假设。

  1. You always have more than 1 radio button in the set
  2. Your form is the 1st (index 0) form... if not you'll need to adjust the radioSet lookup
  1. 您的集合中总是有 1 个以上的单选按钮
  2. 您的表单是第一个(索引 0)表单...如果不是,您需要调整 radioSet 查找