我查看了其他线程,但找不到解决方案,但我有一个表单可以正确提交到 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/