如何在单击下一步时获得所选答案

How to get the selected answer when click next

本文关键字:答案 单击 下一步      更新时间:2023-09-26

是否可以在以下表单中单击下一步后获得所选答案?我尝试在没有结果的on.click模块中使用javascript。我认为问题是在名称值,因为是php代码。

if($i==1){  ?>   
                <div id='question<?php echo $i;?>' class='cont'>
                <?php //echo $ident;
                //echo $respuesta;
                $respuesta = $result['answer'];
                //echo $respuesta1; ?>   
                <p class='questions' id="qname<?php echo $i;?>"> <?php echo $i?>. <?php echo $result['question_name'];?></p>
                <input type="radio" value="1" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'/> <?php echo $result['answer1'];?>
               <br/>
                <input type="radio" value="2" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'/> <?php echo $result['answer2'];?>
                <br/>
                <input type="radio" value="3" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'/> <?php echo $result['answer3'];?>
                <br/>
                <input type="radio" value="4" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'/> <?php echo $result['answer4'];?>
                <br/>
                <input type="radio" checked='checked' style='display:none' value="5" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'/>  
                <br/>
                <button id='<?php echo $i;?>' class='next btn btn-success' type='button'>Siguiente</button>
                </div>     
                 <?php }elseif($i<1 || $i<$rows){?>
                   <div id='question<?php echo $i;?>' class='cont'>
                <p class='questions' id="qname<?php echo $i;?>"><?php echo $i?>.<?php echo $result['question_name'];?></p>
                <input type="radio" value="1" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'/> <?php echo $result['answer1'];?>
                <br/>
                <input type="radio" value="2" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'/> <?php echo $result['answer2'];?>
                <br/>
                <input type="radio" value="3" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'/> <?php echo $result['answer3'];?>
                <br/>
                <input type="radio" value="4" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'/> <?php echo $result['answer4'];?>
                <br/>
                <input type="radio" checked='checked' style='display:none' value="5" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'/>      
                <br/>
                <button id='<?php echo $i;?>' class='previous btn btn-success' type='button'>Anterior</button>                    
                <button id='<?php echo $i;?>' class='next btn btn-success' type='button' >Siguiente</button>
                </div>

               <?php }elseif($i==$rows){?>
                <div id='question<?php echo $i;?>' class='cont'>
                <p class='questions' id="qname<?php echo $i;?>"><?php echo $i?>.<?php echo $result['question_name'];?></p>
                <input type="radio" value="1" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'/> <?php echo $result['answer1'];?>
                <br/>
                <input type="radio" value="2" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'/> <?php echo $result['answer2'];?>
                <br/>
                <input type="radio" value="3" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'/> <?php echo $result['answer3'];?>
                <br/>
                <input type="radio" value="4" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'/> <?php echo $result['answer4'];?>
                <br/>
                <input type="radio" checked='checked' style='display:none' value="5" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'/>                                                                      
                <br/>
                <button id='<?php echo $i;?>' class='previous btn btn-success' type='button'>Anterior</button>                    
                <button id='<?php echo $i;?>' class='next btn btn-success' type='submit'>Terminar</button>
                </div>
                <?php } $i++;} ?>
            </form>

我的脚本如下:

<script>
$('.cont').addClass('hide');
count=$('.questions').length;
 $('#question'+1).removeClass('hide');
 $(document).on('click','.next',function(){
     last=parseInt($(this).attr('id'));
     nex=last+1;
 numeroresp = window["varjs" + last];
    $('#question'+last).addClass('hide');
     $('#question'+nex).removeClass('hide');
 });
 $(document).on('click','.previous',function(){
            last=parseInt($(this).attr('id'));     
             pre=last-1;
             $('#question'+last).addClass('hide');
             $('#question'+pre).removeClass('hide');
         });        
</script>

在jquery中可以这样做:

$("input[type='radio'][name='your radio button name property']:checked")[0].id

在javascript中你可以这样做:

var r = document.getElementsByName('your radio button name property');
    for (var i = 0; i < r.length; i++) {
        if (r[i].checked) {
            alert(r[i].id);
            break;
        }
    }

它会给你单选按钮的id。

标记为回答