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


i have form of survey questions. first hide questions , show 1 after button next. i've made validation , works last question not working. i've provided php validation if doesn't have javascript enabled. when have not chosen answer last question,it not submit form, returns first question of survey , have choose them again. that's problem - how validate last quesion? here's code:

$(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 $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>​ 

i tried this:

$(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++;          }      }); });​ 

it prevents form submit, have selected answer, form not submit. how make first condition if have chosen answer, form subit? run second condition , form not submit. thanks!

you can prevent form submitted following code:

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

you can define allfieldsarevalid true or false when fields filled correctly or not.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -