Ajax fade mesaj si redirect dupa un timp

Discutii si intrebari legate de scripturi si functii JavaScript, jQuery si Ajax, cod JavaScript in general.
royal27
Mesaje: 66

Ajax fade mesaj si redirect dupa un timp

Am codul asta si vreau in loc de input type=button sa fie submit, dar sa imi dea fade la fel in 5 secunde de cand apare eroarea.
Daca schimb cu input type=submit imi apare pentru o secunda si dispare.
Dupa ce arata mesajul de succes vreau sa ma redirectioneze catre aceeasi pagina.

Cod: Selectaţi tot

       <form class="account-form" id="submit_form" method="post">
     <span id="error_message" class="text-danger"></span>  
			<span id="success_message" class="text-success"></span>  
               <fieldset>
                    <i class="ti-key"></i>
                    <input type="password" name="pass" id="pass"  placeholder="Password" />
               </fieldset>
               <input type="button" value="Login" id="submit" class="red-bg" />
          </form>
           </div>  
  
 <script>  
 $(document).ready(function(){  
      $('#submit').click(function(){  
 
           var pass = $('#pass').val();  
           if( pass == '')  
           {  
                $('#error_message').html("All Fields are required");  
           }  
           else  
           {  
                $('#error_message').html('');  
                $.ajax({  
                     url:"/unlock",  
                     method:"POST",  
                     data:{pass:pass},  
                     success:function(data){  
                          $("form").trigger("reset");  
                          $('#success_message').fadeIn().html(data);  
                          setTimeout(function(){  
                               $('#success_message').fadeOut("Slow");  
                          }, 5000);  
                     }  
                });  
           }  
      });  
 });  
 </script> 

MarPlo
Pune type="submit" la buton, si foloseste urmatorul script:

Cod: Selectaţi tot

<script>  
$(document).ready(function(){  
  $('#submit_form').click(function(ev){
    ev.preventDefault();  //to not submit the form

    var pass = $('#pass').val();  
    if( pass == '') $('#error_message').html("All Fields are required");
    else {
      $('#error_message').html('');
      $.ajax({
        url:"/unlock",
        method:"POST",
        data:{pass:pass},
        success:function(data){
          $("form").trigger("reset");
          $('#success_message').fadeIn().html(data);
          setTimeout(function(){
            $('#success_message').fadeOut("Slow");
            location.reload();  //to reload the page
          }, 5000);
        }
      });
    }  
  });  
});  
</script>

royal27
Am rezolvat,multumesc frumos de ajutor

Subiecte similare