简体   繁体   中英

jquery radio button validation is not working

I developed one quiz application in php..In that, i display one question per page..by using next button user able to see next question.

What suppose to happen is that if none of the radio buttons have been selected then it should come up with a message stating 'please select your answer'.

so my problem is it works only for the first question in my application..when I click on next button the alert is not working from the second page.

please help me how to do this. thank you.

var count = 0;

$('body').on('click','.Next',function(e){
    e.preventDefault();

    var question_id = $(this).data('id');
    var formId = "question_form"+question_id;
    var value = $(this).val();
    var timer1 = $("#timer1").val();
    var timer2 = $("#timer2").val();

    var answer = $('#'+formId).find("input[class=radio]:checked").val();


    if(!$('#'+formId).find("input[class=radio]:checked").val()){
        alert('please select your answer');
        return false;
    }else{
        $.ajax({
            type: 'post',
            url: 'scores.php',
            data:{ "Question":question_id, "Answer":answer, "Timer1":timer1, "Timer2":timer2},
            success: function(data){
                alert(data);
                change_next(value);
                //console.log(data);
                $("#timer1").val(0);
                $("#timer2").val(0);            
            },
            beforeSend: function(){
                $('#loading').show();       
            }
        });
    }

 restartTimer();
    count--;
    count--;

}); 

Try this..

var question_id = $(this).data('id');
    var value = $(this).val();
    var timer1 = $("#timer1").val();
    var timer2 = $("#timer2").val();
    var answer=$("input[class=radio]:checked").val();

      if(!$('input[name=radio]:checked').length<=0)
           {
            alert('please select your answer');
            //return false;
           }

         else
        {

     $.ajax({
        type: 'post',
        url: 'scores.php',
        data:{ "Question":question_id, "Answer":answer, "Timer1":timer1, "Timer2":timer2},
        success: function(data)
        { 
            alert(data);
            change_next(value);
            //console.log(data);
            $("#timer1").val(0);
            $("#timer2").val(0);

        },
        beforeSend: function(){
        $('#loading').show(); 

    }
    });
         }

     restartTimer();
            count--;
            count--;

    }); 

i added my form id to the radio button..so now it works fine.

var answer = $('#'+formId).find("input[class=radio]:checked").val();


  if(!$('#'+formId).find("input[class=radio]:checked").val())
   {
    alert('please select your answer');
    return false;
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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