Очистить форму HTML после отправки с Javascript JS - PullRequest
0 голосов
/ 31 октября 2018

Я просмотрел другие темы и не могу найти решение, но у меня есть форма, которая правильно отправляется в 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.log('Success!', response))  
    .catch(error => console.error('Error!', error.message));
  })  
 </script>  

Ответы [ 3 ]

0 голосов
/ 31 октября 2018

<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>

После успеха очистите форму, используя селектор формы.

0 голосов
/ 31 октября 2018

мы можем сбросить форму с помощью 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>
0 голосов
/ 31 октября 2018

просто добавьте это:

Добавить форму: id = "myForm"

        document.getElementById("myForm").reset();
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...