javascript - 使用 Javascript js 提交后清除 HTML 表单

标签 javascript html json forms

我查看了其他线程,但找不到解决方案,但我有一个表单可以正确提交到 Google 表格,但输入在按提交后保留内容。

代码如下:

<form name="rvcasole-formresponses">

  <p class="text name">FIRST NAME</p>
  <input type="text" name="firstName" class="_input firstName">

  <p class="text name">LAST NAME</p>
    <input type="text" name="lastName" class="_input lastName">

  <p class="text email">EMAIL</p>
    <input type="text" name="email" class="_input email">

  <p class="text phone">PHONE</p>
    <input type="text" name="phone" class="_input phone">

  <button type="submit" class="button">SUBMIT</button>
</form>

<script>  
  const scriptURL = 'https://script.google.com/macros/s/AKfycbydQrV11QKRWOJ-HPTlEsVaZa1Z3z15DAN6It5k42b8voWDO4w/exec';
  const form = document.forms['rvcasole-formresponses'];
  form.addEventListener('submit', e => {  
   e.preventDefault();
   fetch(scriptURL, { method: 'POST', body: new FormData(form)})  
    .then(response => console.log('Success!', response))  
    .catch(error => console.error('Error!', error.message));
  })  
 </script>  

最佳答案

我们可以使用document.getElementById('formresponses').reset()来重置表单

const scriptURL = 'https://script.google.com/macros/s/AKfycbydQrV11QKRWOJ-HPTlEsVaZa1Z3z15DAN6It5k42b8voWDO4w/exec'  
  const form = document.forms['rvcasole-formresponses']  
  form.addEventListener('submit', e => {  
   e.preventDefault()  
   fetch(scriptURL, { method: 'POST', body: new FormData(form)})  
    .then(response => document.getElementById('formresponses').reset())  
    .catch(error => console.error('Error!', error.message))  
  }) 
<form name="rvcasole-formresponses" id="formresponses">

    <p class="text name">FIRST NAME</p>
    <input type="text" name="firstName" class="_input firstName">

  <p class="text name">LAST NAME</p>
    <input type="text" name="lastName" class="_input lastName">

  <p class="text email">EMAIL</p>
    <input type="text" name="email" class="_input email">

  <p class="text phone">PHONE</p>
    <input type="text" name="phone" class="_input phone">

  <button type="submit" class="button">SUBMIT</button>
</form>

关于javascript - 使用 Javascript js 提交后清除 HTML 表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53082203/

相关文章:

java - 绕过 Gson 数字解析器实现

javascript - 计算一直给出 NaN 结果

javascript - 谷歌闭包 : Passing 'this' into window. setInterval

android - obj.getJSONArray 未获取 JSON 响应

python - 过滤掉 HTML 标签并解析 python 中的实体

html - 响应式设计 - DOM 元素限制和 HTML 与 SVG 性能

java - 使用Gson在java中解析JSON

javascript - 带有下拉列表的搜索栏

javascript - 从嵌套的 json 中获取键的值

javascript - 通过外部 .js 文件内联显示列表元素