dongmo6937 2013-04-30 05:21
浏览 24
已采纳

使用jquery显示表单结果

I got a form, with 4 questions (1 question has 4 radio buttons), each question is stored inside a div.

With jquery i first show the 1st div, when i press the next button i show the 2nd, and so on.

Heres the entire code for it:

    <form style="position:absolute; margin-left:140px;" method="post">
        <div id="question1">
            Q1
            <br/>
            <input name="q1" type="radio" value="q1a1">
            A1
            <br/>       
            <input name="q1" type="radio" value="q1a2">
            A2      
            <br/>
            <input name="q1" type="radio" value="q1a3"> 
            A3  
            <br/>
            <input name="q1" type="radio" value="q1a4"> 
            A4
            <br/>
        </div>

        <div id="question2">
            Q2
            <br/>
            <input name="q2" type="radio" value="q2a1">
            A1
            <br/>       
            <input name="q2" type="radio" value="q2a2">
            A2      
            <br/>
            <input name="q2" type="radio" value="q2a3"> 
            A3  
            <br/>
            <input name="q2" type="radio" value="q2a4"> 
            A4
            <br/>
        </div>

        <div id="question3">
            Q3
            <br/>
            <input name="q3" type="radio" value="q3a1">
            A1
            <br/>       
            <input name="q3" type="radio" value="q3a2">
            A2      
            <br/>
            <input name="q3" type="radio" value="q3a3"> 
            A3  
            <br/>
            <input name="q3" type="radio" value="q3a4"> 
            A4
            <br/>
        </div>

        <div id="question4">
            Q4
            <br/>
            <input name="q4" type="radio" value="q4a1">
            A1
            <br/>       
            <input name="q4" type="radio" value="q4a2">
            A2      
            <br/>
            <input name="q4" type="radio" value="q4a3"> 
            A3  
            <br/>
            <input name="q4" type="radio" value="q4a4"> 
            A4
            <br/>
        </div>
        <br/><br/><br/><br/>
        <input type="button" id="submit" name="Submit" />
    </form>
<button id="next">Next question</button>
<script>
$('#submit').hide();
$('div[id^="question"]').hide().first().show();
    $("#next").click(function (e) {
        event.preventDefault();
        $('div[id^="question"]:visible').hide().next().show();
    });
</script>

This is what i want - when the last (4th) question loads, i want my next button to change into submit button. When i press the submit button it would show witch radio buttons were selected. Any suggestions on how can i do this?

  • 写回答

1条回答 默认 最新

  • dragonhong641016 2013-04-30 05:27
    关注

    Try something like this:

    var currentQuestion = 1;
    $(document).ready(function() {
        $('.next').click(function(e) {
            e.preventDefault();
            if(currentQuestion < 4) {
                $('#question' + (currentQuestion).toString()).hide();
                $('#question' + (currentQuestion + 1).toString()).show();
                currentQuestion++;
            }
            else { // On question four, process the form
                // Not sure what you want to do with the data, but 
                // you can parse them like this:
                var selections = {};
                for(var i=1;i<=4;i++) {
                    selections[i] = $('input[name="q' + i.toString() + '"]:checked').val()
                }
    
                // Then you have a JS object with your questions and 
                // corresponding choices, so you can do what you want.
            }
       });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数