doucitao2944 2017-10-17 07:56
浏览 74

如何使用jquery或php根据之前选择的响应选择单选按钮?

I am creating an online quiz system, where i am taking questions along with there options from getquestion.php using ajax on quiz.html, i have added next and previous button on quiz.html, when user select an answer and hit next it goes to the second question, but when user hit previous button i want to show the previously selected option which user selected before hit next button. i tried but code is not working please help me i am new with this.

quiz.html

<div id='questionArea' style="position:absolute;top:30%;left:1%;"></div>
<button id='next' type="submit" style="position:absolute;top:50%;left:1%;padding:5px;width:80px;background-color:silver;border:1px solid grey;color:white;border-radius:20%;cursor:pointer;">Next</button><button id='prev' type="submit" style='display: none;position:absolute;top:50%;left:10%;padding:5px;width:80px;background-color:silver;border:1px solid grey;color:white;border-radius:20%;cursor:pointer;'>Previous</button>

<script>
$(document).ready(function() {
var number = 0;
 $.post('getquestion.php', {number: number}, function(result){

        $('#questionArea').html(result);
    });

$('#next').click(function() {
number = number + 1;

    if (number => 1){
$('#prev').css('display','block');
}

    $.post('getquestion.php', {number: number}, function(result){

        $('#questionArea').html(result);
    });

});

$('#prev').click(function() {
number = number - 1;
    if (number < 1){
$('#prev').css('display','none');
}

    $.post('getquestion.php', {number: number}, function(result){

        $('#questionArea').html(result);
    }); 
});
});

getquetion.php

                if(isset($_POST['number'])){
            $number = $_POST['number'];
            }
            $sql="SELECT * FROM `questions`";
            $result=mysqli_query($conn,$sql);
            $qid = array();
            $question = array();
            $chA = array();
            $chB = array();
            $chC = array();
            $chD = array();
            while ($row = mysqli_fetch_assoc($result)) {
            $qid[] = $row['question_id'];
            $question[] = $row['question'];
            $chA[] = $row['ansa'];
            $chB[] = $row['ansb'];
            $chC[] = $row['ansc'];
            $chD[] = $row['ansd'];
            $correct = $row['correct_ans'];
            }



            echo "<div id='question'>".$qid[$number].")"." ". $question[$number] . "</div>";
            echo "<input id='a' type='radio' name='a' value='" . $chA[$number] ."'> " . $chA[$number] . "<br>";
            echo "<input id='b' type='radio' name='a' value='" . $chB[$number] ."'> " . $chB[$number] . "<br>";
            echo "<input id='c' type='radio' name='a' value='" . $chC[$number] ."'> " . $chC[$number] . "<br>";
            echo "<input id='d' type='radio' name='a' value='" . $chD[$number] ."'> " . $chD[$number] . "<br>";




            ?>
            <!doctype html>
            <html>
            <head></head>
            <body>
            <script>
            var value;

            $("#a").click(function(){
                value = $(this).val();


            });
            $("#b").click(function(){
                value = $(this).val();

            });
            $("#c").click(function(){
                value = $(this).val();


            });
            $("#d").click(function(){
                value = $(this).val();


            });

            $('#prev').click(function(){
                if(value == "something that user selects before"){
                    $('#').prop("checked",true);
                }else{
                    $('#').prop("checked",false);
                }
            });
            </script>
</div>
  • 写回答

1条回答 默认 最新

  • duanduoding2238 2017-10-17 08:15
    关注

    you could store answers in session for that question like

    $_SESSION["Q3"] = 3;
    

    and then check if session is set and if yes make this

    echo "<input id='d' type='radio' name='a' value='" . $chD[$number] ."'> " . $chD[$number] . "<br>";
    

    into

    echo "<input CHECKED id='d' type='radio' name='a' value='" . $chD[$number] ."'> " . $chD[$number] . "<br>";
    

    this.

    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法