如果没有答案,如何防止提交表单的最后一个问题

How to prevent submit last questionof a form if there is no answer

我有一份带问题的调查表。首先,我隐藏所有问题,然后使用“下一步”按钮一个接一个地显示。我已经进行了验证并且它有效但仅针对最后一个问题它不起作用。如果有人没有启用 JavaScript,我还提供了 Php 验证。现在当你没有为最后一个问题选择答案时,它不会提交表格,但它 returns 到调查的第一个问题,你必须重新选择它们。那是我的问题 - 如何验证最后一个问题?这是我的代码:

$(function () {
    $( document ).ready(hideAllRows);
    $( document ).ready(showFirst);
    $('#button').hide();
});

function hideAllRows(){
    $('#questionsTable tr').each(function() {
        $(this).hide();
    });
}

$(function () {
    $("#next").click(showNextQuestion);
});

var currentInd = 1;
var prevInd = 1;
function showNextQuestion(){
    $(function () {
        var indId = "#"+currentInd;
        prevInd = currentInd - 1;
        var prevId = "#"+prevInd;

        //validation
        var isAnyClicked = false;​
        if ($(prevId + " input[type='radio']:checked").val()) {
            isAnyClicked = true;
        }

        if(currentInd > 0 && currentInd  != $('#questions_count').val() && isAnyClicked == false) {
            alert('Моля, отговорете!');
        }

        else {
            //hide previous question
            $(prevId).hide();

            //show next question 
            var indId = "#"+currentInd;
            $(indId).show();
            currentInd++;
            if(currentInd  == $('#questions_count').val()) {
                $('#button').show();
                $("#next").hide();
            }
        }
    });
}

function showFirst(){
    $(function () {
        $('#0').each(function() {
            $(this).show();
        });
    });
}

.

<?php

$att=array('id'=>'form');
echo form_open('index/survey_fill/' .$survey_id .'/'. $question_id , $att); ?>
<input type='hidden' name='questions_count' id='questions_count' value='<?php echo count($question); ?>' />
<table id='questionsTable' >
  <thead>
    <tr><th>Question</th></tr>
  </thead>   
  <tbody>
  <?php
    echo validation_errors();​
 $index = 0;
    foreach ($question as $row)
    {
        echo "<tr id='$index'>";
        $index++;
    ?>

        <td> 
        <?php echo "$row->question"; ?><br/>
        <?php echo "<input type='hidden' name='question_id' value='$row->question_id' />"; ?>
        <?php 

        $data=array(
            'name' =>  'answer['.$row->question_id.']',
            'value' => '5',
            'class' => 'answer'

        );

        echo "<input type='hidden' name='survey_id' value='$row->survey_id'>"; 

        echo form_radio($data); 
        echo " 5 ";
        $data=array(
            'name' =>  'answer['.$row->question_id.']',
            'value' => '4',
            'class' => 'answer'
        );
        echo form_radio($data);​
         echo "4";
</td></tr>

    <?php 
    }

?>      

</table>
 <?php echo "<input type='hidden' name='question_id' value='$row->question_id' />"; ?>
  <?php  echo '<input type="submit" id="button" name = "submit" value="Submit" class="btn btn-success">';
  ?>
<input type="button" id="next" name = "next" value="Next" class="btn btn-success">
</form>
</div>

</body>​

我试过这个:

$(document).ready(function() {
    var currentInd=1;      
    $('form').on('submit', function(e){
        isAnyClicked = false;

        if (currentInd  = $('#questions_count').val() &&
                ($(currentInd + " input[type='radio']:checked").val())) {
             isAnyClicked = true;
             $(currentInd ).hide();
             alert(" answer selected");
        } if (currentInd  = $('#questions_count').val() && isAnyClicked == false)  {    
            $('tr' + currentInd).css({ display: 'block' });

             e.preventDefault();
             alert("select answer");
             currentInd++;
         } 
    });
});​

它阻止了表单提交,但即使我选择了答案,表单也没有提交。如果您选择了要提交的答案,如何设置第一个条件?它总是 运行 第二个条件和表单不提交。谢谢!

您可以使用以下代码阻止提交表单:

$(document).ready(function() {
  $('form').on('submit', function(e){
    // validation code here
    if(!AllFieldsAreValid) {
      e.preventDefault();
    }
  });
});

无论是否正确填写所有字段,您都可以将 AllFieldsAreValid 定义为 True 或 False。