简体   繁体   English

如何验证单选按钮?

[英]How to validate a radio button?

I am trying to validate a set of radiobuttons that output a message upon submit informing the user that they must make a selection before they continue. 我正在尝试验证一组单选按钮,这些单选按钮在提交时会输出一条消息,通知用户在继续操作之前必须进行选择。 The code below never recognises that the buttons are checked and always outputs the alert message. 下面的代码无法识别按钮是否已选中,并且始终输出警报消息。 How would I adapt this code to make it work? 我将如何修改此代码以使其起作用?

Radiobuttons: 单选按钮:

<input type="radio" name="examtype" value="GCSE"onclick="return confirmation();"/>GCSE

<input type="radio" name="examtype" value="AS" onclick="return confirmation();"/> AS

<input type="radio" name="examtype" value="A2" onclick="return confirmation();"/> A2 </td> </tr>

Function for confirmation onclick: 确认点击的功能:

function confirmation() {

    for (var i=0; i < document.ExamEntry.examtype.length; i++)
       {
       if (document.ExamEntry.examtype[i].checked)
          {
            var answer = confirm(document.ExamEntry.examtype[i].value)

        if (answer){
            document.ExamEntry.examtype[i].checked = true;

        }
        else{
            document.ExamEntry.examtype[i].checked = false;


        }
          }
     }

    }

Code currently being used for rejecting unselected radiobuttons: 当前用于拒绝未选中的单选按钮的代码:

if (document.ExamEntry.examtype.checked){
      alert('checked') //for testing if statement runs when appropriate//
} 

else {
msg+="You must enter the exam type \n";
result = false;
}

Here's an example code on how to validate radioboxes: http://jsfiddle.net/jyUAM/3/ 以下是有关如何验证单选框的示例代码: http : //jsfiddle.net/jyUAM/3/

<script type="text/javascript">
function validateRadioboxesByName(form, name)
{
    var el = form[name];
    for (i = 0; i < el.length; ++i) {
        if (el[i].checked)
            return true;
    }

    return false;
}

function checkRegistration() {
    var isValid = validateRadioboxesByName(document.forms.test, "examtype");

    if (!isValid)
        alert("Please check one of the radiobox.");

    return isValid;
}
</script>

<form name="test" onsubmit="return checkRegistration()">
    <input type="radio" name="examtype" value="GCSE"/>GCSE

    <input type="radio" name="examtype" value="AS"/> AS

    <input type="radio" name="examtype" value="A2"/> A2

    <button type="submit">Send</button>
</form>

This way you don't need to remember the state of a clicked radiobox, but just ask the browser when you need the data, that is, before submitting the data. 这样,您无需记住单击的单选框的状态,而只需在需要数据时即在提交数据之前询问浏览器。

This tutorial may give you more ideas on how to visualize the message to the user instead of using an ugly alert call: http://www.the-art-of-web.com/html/html5-checkbox-required/ 本教程可以为您提供更多有关如何可视化显示给用户的消息的想法,而不是使用丑陋的alert电话: http : //www.the-art-of-web.com/html/html5-checkbox-required/

That the examples on the last link are for checkboxes shouldn't scare you, you can easily apply the same to radio- input-boxes and the like. 最后一个链接上的示例是针对复选框的,不要吓到您,您可以轻松地将其应用于单选框等。

Try setting a variable as false outside both functions, and then if the confirmation function runs, assign that variable to true. 尝试在两个函数之外都将变量设置为false,然后如果确认函数运行,则将该变量分配为true。

Then in your next function where you check if the user have clicked a radio button or not, use an if statement that runs only if the variable has been set to false which could include your alert message. 然后在下一个检查用户是否单击单选按钮的函数中,使用if语句仅在变量已设置为false时才运行,该变量可能包括您的警报消息。

Hope this helps :) 希望这可以帮助 :)

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

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