单选按钮的验证脚本不工作

时间:2014-05-16 15:48:31

标签: javascript jquery html5 arraylist

我收到错误。该脚本无法正常工作。救命 提前致谢。 我想检查下一个按钮单击时是否检查了所有单选按钮。 其他方式也欢迎

 <div class="qheader">
 9) What's the world's most widely spoken language?</div>
 <div class="qselections">
 <input type="radio" value="a" name="question9">a) English<br>
 <input type="radio" value="b" name="question9">b) Spanish<br>
 <input type="radio" value="c" name="question9">c) Mandarin<br>
 <input type="radio" value="d" name="question9">d) French<br>
 </div><br>

 <div class="qheader">
 10) Which continent is host to the most countries in the world?</div>
 <div class="qselections">
 <input type="radio" value="a" name="question10">a) Asia<br>
 <input type="radio" value="b" name="question10">b) Africa<br>
 <input type="radio" value="c" name="question10">c) Europe<br>
 </div>
 </div>
 <input type="button" value="Next" name="B3" onclick="showdesc()">

 <script>
        $(function() {
            $(document).on('click', 'form', function () {

                var validate = true;
               var unanswered = new Array();

               // Loop through available sets
               $('.qselections').each(function () {
                // Question text
                var question = $(this).prev().text();
                // Validate
                if (!$(this).find('input').is(':checked')) {
                    // Didn't validate ... dispaly alert or do something
                    unanswered.push(question);

                    validate = false;
                }
            });

            if (unanswered.length > 0) {
                msg = "Please answer the following questions:\n" +         unanswered.join('\n'); 
                alert(msg);
            }
            return validate;
        });
    });
    </script>

2 个答案:

答案 0 :(得分:0)

    //try this script

    if(!$(this).find("input[name=radio]").is(':checked')){
        alert("hi");
    }

答案 1 :(得分:0)

function clicked() {

            var validate = true;
           var unanswered = new Array();

           // Loop through available sets
           $('.qselections').each(function () {
            // Question text
            var question = $(this).prev().text();
            // Validate
            if (!$(this).find('input').is(':checked')) {
                // Didn't validate ... dispaly alert or do something
                unanswered.push(question);

                validate = false;
            }
        });

        if (unanswered.length > 0) {
            msg = "Please answer the following questions:\n" +         unanswered.join('\n'); 
            alert(msg);
        }
        return validate;
    }

JSFiddle

我稍微修改了你的问题,对我来说效果很好。我想这只是代码部分的语法错误,而不是小提琴中的错误。交叉检查,让我知道。 尝试并修改您的点击通话,使其像小提琴一样直接。