简体   繁体   中英

How to clean form after submitting ajax?

First of all: I tried to search the answer in the web and I found like 20 examples of code. But I failed. still nothing is working.

I'm making a simple chat and i need to clean my form after submitting data via ajax. Here is the code:

<form id="ChatFrom" class="chatMessageField" action="Amess.php" method="post">
    <input class="chatMessageField" type="text" name="mess" /><br />
    <input class="chatMessageBtn" type="button" value="Отправить" onclick="SendForm();" />
</form>

<script type="text/javascript" src="http://scriptjava.net/source/scriptjava/scriptjava.js"></script>
<script type="text/javascript">
    function SendForm() {
        $$f({
            formid:'ChatFrom',
            url:'Amess.php',
        });
    }
</script>
$("#ChatFrom")[0].reset()
    // or
 $("#ChatFrom").get(0).reset()

You could do it like this:

function SendForm() {
   $.post( "Amess.php", function( data ) {
      resetFields();
   });
}


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

Hope it helps!

$(".chatMessageField").val(''); is a simple way, but you have to do it to all the fields individually. A better solution would be to give all of your inputs a class name that is the same and clear them all at once. Or just use jquery reset()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM