简体   繁体   English

验证是否选择了单选按钮组JQUERY

[英]Validating if the radio button group is selected JQUERY

I'm trying to validate the radio button group,. 我正在尝试验证单选按钮组。 if not check the span will have a text which indicates that the radio button must be selected,. 如果未选中,则跨度将显示一个文本,指示必须选中单选按钮。 the problem is,. 问题是,。 if I place the codes of radion button validation on top, it does not work, but when it is below,. 如果我将单选按钮验证的代码放在顶部,则不起作用,但是在下面时。 it works.. Kinda weird,. 它有效.. Kinda很奇怪,。 any idea for this one? 这个有什么想法吗? thanks 谢谢

    $("#mchoice").submit(function () {

        var direction =  $('#direction').val();

        var quiztxtBox =  document.getElementsByName('quiztxtBox[]');

        var isSubmit;
        var names = [];
        var err = document.getElementsByName('errMchoice[]');


                    // For radio button answers.        
       $('input[type="radio"]').each(function(){
          names[$(this).attr('name')] = true; 
          });      

          if (!direction) 
          {
            $('#direction').focus();
            $('#direction').css({"background-color":"#f6d9d4"});
            $('#direction').nextAll('span').html('Type in direction.');
            event.preventDefault();
          }
          else
          {
            $('#direction').css({"background-color":"#fff"});
            $('#direction').nextAll('span').html("");
          }


        for(correct_answer in names)
        {
            var radio_buttons = $("input[name='" + correct_answer + "']");

            if( radio_buttons.filter(':checked').length == 0)
            {

                radio_buttons.nextAll('span').html('Select the answer.');
                event.preventDefault();
            }
           else
            {
                radio_buttons.nextAll('span').html('');
            }
        }

        // Choices fields

        $("[name='quiztxtBox[]']").each(function(){


         if (!this.value.length) 
          {
            $(this).css({"background-color":"#f6d9d4"}).siblings('span.errorMsg').text('Please type in question/answer!');

            event.preventDefault();
          }
          else
          {
            $(this).css({"background-color":"#fff"}).siblings('span.errorMsg').text("");

          }


        });   
}); 

HTML here HTML在这里

<div id="QuestionTBDiv1" >
                                        <label>Question</label><br/>
                                        <input type="text" name="quiztxtBox[]" size="57" id="quiztxtBox[]" placeholder="Question #1"><br/>
                                        <label>Answer</label><br/>
                                        <input type="text" name="quiztxtBox[]" size="24" id="answer[]" placeholder="Choice A">&nbsp;<input type="radio" class = "choiceA" name="correct_answer1" value="A">&nbsp;
                                        <input type="text" name="quiztxtBox[]" size="24" id="answer[]" placeholder="Choice B">&nbsp;<input type="radio" class = "choiceB" name="correct_answer1" value="B"><br/>
                                        <input type="text" name="quiztxtBox[]" size="24" id="answer[]" placeholder="Choice C">&nbsp;<input type="radio" class = "choiceC" name="correct_answer1" value="C">&nbsp;
                                        <input type="text" name="quiztxtBox[]" size="24" id="answer[]" placeholder="Choice D">&nbsp;<input type="radio" class = "choiceD" name="correct_answer1" value="D"><br>
                                        <span name="errMchoice" class="errorMsg"></span>
                                        </div>

JsFiddle: http://jsfiddle.net/Ej77L/2/ JsFiddle: http : //jsfiddle.net/Ej77L/2/

There was a small logical error in your javascript. 您的javascript中有一个小逻辑错误。 Once you set the span with error message of 'Select an answer' you are doing a checking if the question has been filled. 一旦将跨度设置为错误消息“选择答案”,您将在检查问题是否已填写。 If it has been filled then you are making the span text empty. 如果已填满,则您要使span text空。

So instead of that, keep a flag to see if the answer was selected or not. 因此,相反,请保留一个标志以查看是否已选择答案。 If not selected then set the flag and in later part of the code, don't empty span text 如果未选中,请设置标志,然后在代码的后半部分,不要清空span text

Here's a working DEMO 这是一个有效的演示

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

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