English 中文(简体)
提交后的明确形式价值
原标题:clear form values after submission 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;


        } 
  });

这是我的一种形式。

<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>

在成功提交之后,我需要澄清实地价值观的形式。 如何做到这一点?

最佳回答
$("#cform")[0].reset();

或穿便衣:

document.getElementById("cform").reset();
问题回答

你可以在你的费用范围内这样做。

$.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(  );

            });

使用:

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

或如果你提到<代码>的表格 * E/CN.6/2009/1。

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

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

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

    //...more form stuff...

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

        //...more success stuff...

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

简单

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

它的工作是:在麻省成功之后,把这一职能称为“你”并寄出其表象。 例如:

This function clear all input fields value including button, submit, reset, hidden fields

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

* This function clears all input fields value except button, submit, reset, hidden fields * */

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

例如:

<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>
$.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. 电tool Box.com/jquery-clear-form/

Set id in form when you submitting form

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

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

    </form>

   set in jquery 

   document.getElementById("cform").reset(); 
$( #f或mid).reset();

document.getElementById( f或mid ).reset();

Vanilla!

I know this post is quite old.
Since OP is using jquery ajax this code will be needed.
But for the ones looking for vanilla.

    ...
    // Send the value
    xhttp.send(params);
    // Clear the input after submission
    document.getElementById( cform ).reset();
}

仅使用就构成标签,如:

  $.ajax({
    type: "POST",
    url: "/demo",
    data: dataString,
    success: function () {
      $("form")[0].reset();
      $("#test").html("<div id= message ></div>");
      $("#message")
        .html("<h2>Contact Form Submitted!</h2>")
        .append("<p>We will be in touch soon.</p>")
        .hide()
        .fadeIn(1500, function () {
          $("#message").append(
            "<img id= checkmark  src= images/check.png  />"
          );
        });
    }
  });
  e.preventDefault();
});

采用“jax reset(a)”方法,在提交后,你可以确定形式。

例如,见以上文:

const form = 文件.getElementById(cform).reset();

If you are using a form tag in your form. Then

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

该守则将完美地运作,但如果你不使用任何形式标签,那么你就能够尝试为每个投入领域设定一个空洞的价值。

$( input[type="text"],textarea ).val(  );




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签