【问题标题】:jquery fetch answer on click submit(sources data from database)jquery 在点击提交时获取答案(从数据库中获取数据)
【发布时间】:2019-03-03 00:02:03
【问题描述】:

你好,我这里有一段代码,它从数据库中获取数据(问题和选项)。我需要帮助的是我想在点击提交按钮时获得所有答案

    <?php
$res = $conn->query("select * from questions where category_id='$category' ORDER BY RAND()") ;
$rows = mysqli_num_rows($res);
$i=1;
while($result=mysqli_fetch_array($res)){?>

    <?php if($i==1){?>        
    <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='next<?php echo $i;?>' class='next btn btn-success' type='button'>Next</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='pre<?php echo $i;?>' class='previous btn btn-success' type='button'>Previous</button>       
        <button id='next<?php echo $i;?>' class='next btn btn-success' type='button' >Next</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='pre<?php echo $i;?>' class='previous btn btn-success' type='button'>Previous</button>       
        <button id='next<?php echo $i;?>' class='next btn btn-success submit' type='submit'>Finish</button>
        </div>
    <?php 

    } 
$i++;
} ?>

</form>

请指导我使用可以在提交时获取所有答案的 jquery 代码。 这是我在下面尝试过的

  $(document).on('click','.submit',function(){
 var answer = [];
$. each($(this.checked), function(){
 answers. push($(this). val());
 });
 alert("your answers are: " + answers. join(", "));

【问题讨论】:

  • $('form').on('submit', function(){ alert(...) })

标签: javascript php jquery html mysql


【解决方案1】:

试试这个:

$('form').on('submit',function(){
    var answer = [];
    $(this).find('input[type="radio"]:checked').each(function(){
        answers.push($(this).val());
    });
    alert("your answers are: " + answers.join(", "));

    //return false to prevent submission
});

这部分

  $(this.checked)

毫无意义,this 在这种情况下是.submit 按钮。所以这并没有告诉我们如何找到复选框/单选按钮。

在任何情况下,在 jQuery 中,我们都可以使用 :checked 来作为复选框/单选按钮,这样您就可以在上面进行操作,或者例如使用 if($(foo).is(':checked'))

相反,我们应该使用表单的提交操作作为我们的事件,因为 this 是包含复选框/单选按钮的表单。这样就更容易找到它们。您可以使用按钮单击事件,但随后您需要执行以下操作:

 //instead of $(this).find('input[type="radio"]:checked').each( ... )
 $('.submit').click(function(){
        $(this).closest('form').find('input[type="radio"]:checked').each( ... );

        //...
 });

因为$(this) 将是按钮,所以我们必须获取父表单,然后获取复选框/单选按钮。

【讨论】:

  • 我的 vs 代码中有所有这些,我无法将其放在 stackoverflow 上。我需要的真正帮助是我想在使用 jquery 提交时获得答案
  • 太棒了!!!我需要得到每个:checked 的答案,看看我做了什么`$('.submit').click(function(){ var answer = []; $(this).closest('form').find('input[type="radio"]:checked').each( answer. push($(this). val()); );
猜你喜欢
  • 2013-06-09
  • 2020-08-30
  • 2012-02-10
  • 2012-08-12
  • 2021-02-03
  • 2020-10-29
  • 1970-01-01
  • 2016-03-13
  • 1970-01-01
相关资源
最近更新 更多