-3

我有一个像这样的小表单聊天,我希望单击提交时该表单不会刷新页面。

<form name="form1" method="post" id="formchat" action="forum_add_1313940.xhtml">
<input name="text" id="text2" max-lenght="1000"></input>
<input type="submit" name="submit" class="submit" value="Gửi" id="chats" />
</form>

和js

<script language="JavaScript">
$('#formchat').submit(function () 
{
  sendformchat(); return false; 
});
</script>

但是当我点击“Gửi”时,页面会刷新。点击“Gửi”页面不刷新怎么办?

4

1 回答 1

0

If you want asynchronous behavior, you should handle the form in an AJAX style, you can read about AJAX here!

here is an example using jquery on your form:

var form = $('#form_chat');

form.find('input[type=submit]').on('click', function(e) {
    e.prevetDefault();
    $.ajax({
      type: "POST",
      url: form.attr('action'),
      data: form.serialize(),
      success: function(response) {
        console.log(response);
      }
    });
});
于 2013-05-06T12:18:32.673 回答