如果在执行jQuery之前没有注册语句

时间:2014-06-08 23:02:20

标签: jquery

我无法找到与此问题有关的任何内容。我有一个按钮来检查多项选择问题的答案,以检查它是否正确,如果是这样,它会使下一个按钮处于活动状态。

但情况并非如此,即使没有,或任何单选按钮被选中,它仍会执行。

编辑更新:现在正在运行。感谢您的所有投入!非常适合!

next_test_section: function(test_id) {

    //VALIDATE  
    if($('#q1-a').is(':checked')) {
        var data = "test_id="+test_id+"&";

        async('get_test_data',data, function(response){
    /** Response success **/

            //$(this).parent().removeClass('has-error');

            $('.button-verify').val('Answer Correct');
            $('.button-next').attr('disabled', false);
            //alert($(':checked').val());
            $('.stage').html(response.html);
            user.update_progress_bar(40);

    }, function(response) {
        /** Response error **/

            //$(this).parent().addClass('has-error');
            if ($('.button-verify').val('Answer Incorrect', function() {
                $('.button-next').attr('disabled', true);
            }));

            //alert($(':checked').val());
            //alert('NEXT Error!!!')
            //<input type="button" class="btn btn-success" name="verify" value="Please answer correctly to proceed." />

        });

    }
},

HTML

                                    <div class="tab-pane" id="tab11">
                                    <div class="row">
                                    <div class="form-group">
                                        <label class="col-md-3 control-label">Question 1 is asked here.</label>
                                        <div class="col-md-9">
                                            <div class="radio">
                                                <label for="radio1">
                                                <input type="radio" id="q1-a" name="q1" value="option1"> Answer 1
                                                </label>
                                            </div>
                                            <div class="radio">
                                                <label for="radio2">
                                                <input type="radio" id="q1-b" name="q1" value="option2"> Answer 2
                                                </label>
                                            </div>
                                            <div class="radio">
                                                <label for="radio3">
                                                <input type="radio" id="q1-c" name="q1" value="option3"> Answer 3
                                                </label>
                                            </div>
                                            <div class="radio">
                                                <label for="radio4">
                                                <input type="radio" id="q1-d" name="q1" value="option4"> Answer 4
                                                </label>
                                            </div>
                                        </div>
                                    </div>
                                    </div>
                                </div>

即使我将有效var设置为false,也知道为什么这个函数会继续进行?

3 个答案:

答案 0 :(得分:2)

尝试

if($('#q1-a').find(':checked').length) var valid = true;

if(typeof(valid) === "boolean" && valid) { ..... }

答案 1 :(得分:1)

您正在尝试检查标识为#q1-a的广播是否已选中,并且您是否这样做了

next_test_section: function(test_id) {
    if ( $('#q1-a').is(':checked') ) {
        var data = "test_id="+test_id+"&";

        async('get_test_data',data, function(response){

            $('.button-verify').val('Answer Correct');
            $('.button-next').prop('disabled', false);

            $('.stage').html(response.html);
            user.update_progress_bar(40);

        }, function(response) {

            $('.button-verify').val('Answer Incorrect');
            $('.button-next').prop('disabled', true);
        });
    }
},

find()仅适用于后代元素,以检查当前元素的使用is(':checked')

答案 2 :(得分:0)

你的变量&#34;有效&#34;不是为虚假价值而启动的。试试这个:

var valid = false; 
if($('#q1-a').find(':checked')){
    valid = true; 
 }

并将所有代码放在jquery文档中Ready :(因此,代码将在Jquery准备就绪后运行。)

$(document).ready(function(){
 /// all you function /// and running code.


});