2012-05-17 11 views
18

Korzystam z poniższego skryptu do sprawdzania poprawności mojego formularza kontaktowego.wyczyść wartości formularza po przesłaniu ajax

//submission scripts 
    $('.contactForm').submit(function(){ 
     //statements to validate the form 
     var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/; 
     var email = document.getElementById('e-mail'); 
     if (!filter.test(email.value)) { 
      $('.email-missing').show(); 
     } else {$('.email-missing').hide();} 
     if (document.cform.name.value == "") { 
      $('.name-missing').show(); 
     } else {$('.name-missing').hide();} 

     if (document.cform.phone.value == "") { 
      $('.phone-missing').show(); 
     } 
     else if(isNaN(document.cform.phone.value)){ 
     $('.phone-missing').show(); 
     } 
     else {$('.phone-missing').hide();} 

     if (document.cform.message.value == "") { 
      $('.message-missing').show(); 
     } else {$('.message-missing').hide();} 

     if ((document.cform.name.value == "") || (!filter.test(email.value)) || (document.cform.message.value == "") || isNaN(document.cform.phone.value)){ 
      return false; 
     } 

     if ((document.cform.name.value != "") && (filter.test(email.value)) && (document.cform.message.value != "")) { 
      //hide the form 
      //$('.contactForm').hide(); 

      //show the loading bar 
      $('.loader').append($('.bar')); 
      $('.bar').css({display:'block'}); 

     /*document.cform.name.value = ''; 
     document.cform.e-mail.value = ''; 
     document.cform.phone.value = ''; 
     document.cform.message.value = '';*/ 

      //send the ajax request 
      $.post('mail.php',{name:$('#name').val(), 
           email:$('#e-mail').val(), 
           phone:$('#phone').val(), 
           message:$('#message').val()}, 

      //return the data 
      function(data){ 

       //hide the graphic 
       $('.bar').css({display:'none'}); 
       $('.loader').append(data); 

      }); 

      //waits 2000, then closes the form and fades out 
      //setTimeout('$("#backgroundPopup").fadeOut("slow"); $("#contactForm").slideUp("slow")', 2000); 

      //stay on the page 
      return false; 


     } 
    }); 

To moja forma

<form action="mail.php" class="contactForm" id="cform" name="cform" method="post"> 
    <input id="name" type="text" value="" name="name" /> 
    <br /> 
    <span class="name-missing">Please enter your name</span> 
    <input id="e-mail" type="text" value="" name="email" /> 
    <br /> 
    <span class="email-missing">Please enter a valid e-mail</span> 
    <input id="phone" type="text" value="" name="phone" /> 
    <br /> 
    <span class="phone-missing">Please enter a valid phone number</span> 
    <textarea id="message" rows="" cols="" name="message"></textarea> 
    <br /> 
    <span class="message-missing">Please enter message</span> 
    <input class="submit" type="submit" name="submit" value="Submit Form" /> 
</form> 

muszę wyczyścić wartości pól formularza po złożeniu pomyślnie. Jak mogę to zrobić?

+0

możliwe duplikat [JQuery Wyczyść formularz na blisko] (http://stackoverflow.com/questions/1860675/jquery-clear-form-on-close) – Rafay

+1

ustaw 'val()', aby opróżnić np. '$ ('# name'). val ('')' – deex

Odpowiedz

58
$("#cform")[0].reset(); 

lub zwykły javascript:

document.getElementById("cform").reset(); 
+0

Nie ma potrzeby w "każdym", ponieważ identyfikatory powinny być unikalne. – VisioN

+0

To działa. Dzięki. – designersvsoft

+0

U są całkowicie poprawne – henryabra

1

użyj:

$('form.contactForm input[type="text"],texatrea, select').val(''); 

lub jeśli masz odniesienie do postaci z this:

$('input[type="text"],texatrea, select', this).val(''); 

:input === <input> + <select> s + <textarea> s

+0

+1 w alternatywny sposób. – VisioN

+0

: wejście wybierze 'type = 'submit'' i zresetuje jego wartość, co nie jest pożądane. –

+0

@Joy. Masz rację, naprawiono – gdoron

8

Można to zrobić wewnątrz $ .post nazywa sukces zwrotnego jak ten

$.post('mail.php',{name:$('#name').val(), 
           email:$('#e-mail').val(), 
           phone:$('#phone').val(), 
           message:$('#message').val()}, 

      //return the data 
      function(data){ 

       //hide the graphic 
       $('.bar').css({display:'none'}); 
       $('.loader').append(data); 

       //clear fields 
       $('input[type="text"],textarea').val(''); 

      }); 
+0

Działa również. Dzięki za szybkie odpowiedzi. – designersvsoft

+0

Fantastyczna poprawka. Wielkie dzięki – GhostRider

1
$('.contactForm').submit(function(){ 
    var that = this; 

    //...more form stuff... 

    $.post('mail.php',{...params...},function(data){ 

     //...more success stuff... 

     that.reset(); 
    }); 
}); 
1

Wystarczy

$('#cform')[0].reset(); 
0
$.post('mail.php',{name:$('#name').val(), 
          email:$('#e-mail').val(), 
          phone:$('#phone').val(), 
          message:$('#message').val()}, 

     //return the data 
     function(data){ 
      if(data==<when do you want to clear the form>){ 

      $('#<form Id>').find(':input').each(function() { 
       switch(this.type) { 
         case 'password': 
         case 'select-multiple': 
         case 'select-one': 
         case 'text': 
         case 'textarea': 
          $(this).val(''); 
          break; 
         case 'checkbox': 
         case 'radio': 
          this.checked = false; 
        } 
       }); 
      }  
    }); 

http://www.electrictoolbox.com/jquery-clear-form/

0

Działa: wywołaj tę funkcję po sukcesie ajaxa i wyślij identyfikator formularza w jego parametrach. coś takiego:

Funkcja ta jasno wszystkie pola wprowadzania wartości w tym przycisku, przedkłada, reset, ukryte pola

function resetForm(formid) { 
$('#' + formid + ' :input').each(function(){ 
    $(this).val('').attr('checked',false).attr('selected',false); 
}); 
} 

* Ta funkcja kasuje wszystkie dane wejściowe pola wartości z wyjątkiem przycisku, przedstawienia, Reset, ukrytych pól * */

function resetForm(formid) { 
    $(':input','#'+formid) .not(':button, :submit, :reset, :hidden') .val('') 
    .removeAttr('checked') .removeAttr('selected'); 
    } 

przykład:

<script> 
    (function($){ 
     function processForm(e){ 
      $.ajax({ 
       url: 'insert.php', 
       dataType: 'text', 
       type: 'post', 
       contentType: 'application/x-www-form-urlencoded', 
       data: $(this).serialize(), 
       success: function(data, textStatus, jQxhr){ 
       $('#alertt').fadeIn(2000); 
       $('#alertt').html(data); 
       $('#alertt').fadeOut(3000); 
       resetForm('userInf'); 
      }, 
      error: function(jqXhr, textStatus, errorThrown){ 
       console.log(errorThrown); 
      } 
     }); 

     e.preventDefault(); 
    } 

    $('#userInf').submit(processForm); 
})(jQuery); 

function resetForm(formid) { 
$(':input','#'+formid) .not(':button, :submit, :reset, :hidden') .val('') 
    .removeAttr('checked') .removeAttr('selected'); 
} 
    </script> 
0

Set id w formularzu podczas składania formularza

 <form action="" id="cform"> 

     <input type="submit" name=""> 

    </form> 

    set in jquery 

    document.getElementById("cform").reset(); 
Powiązane problemy