【问题标题】:Submit a form without refreshing page in html, jshtml、js中提交表单不刷新页面
【发布时间】:2013-04-30 03:04:31
【问题描述】:

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

<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”页面不刷新怎么办?

【问题讨论】:

标签: javascript html forms submit page-refresh


【解决方案1】:

如果你想要异步行为,你应该以 AJAX 样式处理表单,你可以阅读 AJAX here

这是在表单上使用 jquery 的示例:

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);
      }
    });
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-17
    • 1970-01-01
    相关资源
    最近更新 更多