我看过其他线程,但找不到解决方案,但是我的表单可以正确提交给Google表格,但是输入内容在按Submit后仍然保留。
代码如下:
<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>
答案 0 :(得分:1)
我们可以使用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>
答案 1 :(得分:0)
只需添加此内容即可:
添加表格:id =“ myForm”
document.getElementById("myForm").reset();
答案 2 :(得分:0)
<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(function(response) {
var frm = document.getElementsByName('rvcasole-formresponses')[0];
frm.reset();
console.log('Success!', response);
})
.catch(error => console.error('Error!', error.message))
})
</script>
成功后,使用表单选择器清除表单。