0

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>
Andrew
  • 4,953
  • 15
  • 40
  • 58
  • 2
    $("#formId")[0].reset() // or $("#formId").get(0).reset() – MH2K9 Aug 08 '14 at 18:08
  • possible duplicate of [How to reset a form using jQuery with .reset() method](http://stackoverflow.com/questions/16452699/how-to-reset-a-form-using-jquery-with-reset-method) – Code Whisperer Aug 08 '14 at 18:13

3 Answers3

1
$("#ChatFrom")[0].reset()
    // or
 $("#ChatFrom").get(0).reset()
MH2K9
  • 11,951
  • 7
  • 32
  • 49
1

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!

squallsv
  • 482
  • 2
  • 11
0

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

Juan Gonzales
  • 1,967
  • 2
  • 22
  • 36
  • Or a jquery each call over the fields then just to a `this.val('')`. would at least be less code. – Leeish Aug 08 '14 at 18:13