How to clear form fields and print a success message after ajax form submission

爷,独闯天下 提交于 2019-12-12 21:03:02

问题


I am using for form submit. I would like to know how can I clear form fields and print a success message after form submission.

I used var options = ( clearForm: true }

but it didn't work

My code :-

        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script> 
    <script src="http://malsup.github.com/jquery.form.js"></script> 

    <script> 
        // wait for the DOM to be loaded 
        $(document).ready(function() { 
            // bind 'myForm' and provide a simple callback function 
            $('#myForm').ajaxForm(function() { 
                //alert("Thank you for your comment!"); 
            }); 

        }); 
    </script> 

</head>

<body>
    <form id="myForm" name="myForm" action="comment.php" method="post" enctype="multipart/form-data"> 
        <input type="text" name="name" />
        <br /> 
        <textarea name="comment"></textarea> 
        <br />
        <input type="file" value="Share a Pic" name="file" id="file" />
        <br />
        <input type="submit" value="Submit Comment" /> 
    </form>
</body>

回答1:


Just guessing, but would'nt the native form.reset() do that ?

$(document).ready(function() { 
     $('#myForm').ajaxForm(function() { 
         this.reset();
         alert('success');
     }); 
}); 



回答2:


You can use resetForm for this. Here is the docs http://malsup.com/jquery/form/#options-object

or cna use functions like resetForm and clearForm docs on http://malsup.com/jquery/form/#api

Or try simply:

$(document).ready(function() { 
     $('#myForm').ajaxForm(function() { 
         success:function(){
               $('#myForm').reset();
         }
     }); 
}); 



回答3:


Try using clearForm(); also resetForm();.

Official Document : http://malsup.com/jquery/form/#api

<script type="text/javascript">
    $(document).ready(function() { 
        // bind 'myForm' and provide a simple callback function 
        $('#myForm').ajaxForm(function() { 
            //alert("Thank you for your comment!"); 
            $(this).clearForm();
        }); 

    }); 
</script> 



回答4:


Should be :

 $(document).ready(function() { 
      $("#myForm").submit(function(){
         $("input[type='text']").val('');
         $("input[type='textarea']").val('');
         alert('Message');
      });
 });



回答5:


It worked with the following code :-

<script type="text/javascript">
        $(document).ready(function() {
            var options = { 
                clearForm: true,
                resetForm: true
            };
            // bind 'myForm' and provide a simple callback function 
            $('#myForm').ajaxForm(function() { 
                //alert("Thank you for your comment!"); 

            }); 
            $('#myForm').ajaxForm(options);

        }); 
    </script> 

Thanks for the help



来源:https://stackoverflow.com/questions/15085937/how-to-clear-form-fields-and-print-a-success-message-after-ajax-form-submission

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!