Я просмотрел другие темы и не нашел решения, но у меня есть форма, которая правильно отправляется в Google Sheet, но входные данные сохраняют содержимое после нажатия кнопки «Отправить».
Код ниже:
<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.info('Success!', response))
.catch(error => console.error('Error!', error.message));
})
</script>
просто добавьте это:
Добавить форму: id = "myForm"
document.getElementById("myForm").reset();
<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.info('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>
Добро пожаловать в SO. Я не вижу попыток стереть значения полей ...