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

时间:2016-08-11 10:09:34

标签: javascript php jquery

点击下面的表单后,可以获得所选答案吗?我尝试在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>

1 个答案:

答案 0 :(得分:0)

在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。

如果它回答你的问题,请标记答案。