跳转到内容
Formhook
← 返回博客

博客

AJAX 表单提交:无需刷新页面即可发送表单

· 1 分钟阅读

const form = document.querySelector("#contact-form");
form.addEventListener("submit", async (e) => {
  e.preventDefault();
  const res = await fetch(form.action, {
    method: "POST",
    body: new FormData(form),
    headers: { Accept: "application/json" },
  });
  if (res.ok) {
    form.reset();
    document.querySelector("#form-status").textContent = "Thanks - we got it.";
  } else {
    document.querySelector("#form-status").textContent = "Something went wrong. Please try again.";
  }
});

一行代码上线可用的表单

欧盟托管,提交记录永久保存,所有套餐均支持推送通知。

免费开始

无需信用卡 · 阅读文档

继续阅读

Cookie 偏好设置

我们使用一个严格必要的会话 cookie 和无 cookie 的第一方分析。在您同意的情况下,我们还会使用 Google Analytics 来了解营销页面的流量。