How to not load the page by clicking on the select option?

Asked

Viewed 87 times

1

I have the following select, every time I select some option it reloads the page, so far so good, but I want to make sure that by clicking the option nAtualiza, it just wouldn’t update the page, is there any way? Thank you!

    <select id="filtro" name="filtro" class="form-control" onchange="this.form.submit()">
       <option value="">Selecione</option>
       <option value="assunto"></option>
       <option value="nAtualiza"></option>
    </select>
  • You want to send data to the server but without reloading the page, that’s it?

2 answers

0


You can do it like this:

document.getElementById('filtro').addEventListener('change', function() {
  if(this.value != 'nAtualiza') {
    console.log('submit');
    this.form.submit();
  }
});
<form> 
<select id="filtro" name="filtro" class="form-control">
   <option value="">Selecione</option>
   <option value="assunto">Assunto</option>
   <option value="nAtualiza">nAtualiza</option>
</select>
</form>

Take out the event onchange of your select and add this javascript to your file.

  • 1

    Thank you!!!!!!!!

0

Add a condition for the execution of your function.

<select id="filtro" name="filtro" class="form-control" onchange="this.value !== 'nAtualiza' && this.form.submit()">
   <option value="">Selecione</option>
   <option value="assunto"></option>
   <option value="nAtualiza"></option>
</select>

The section modified in the code above corresponds to this.value !== 'nAtualiza' && this.form.submit(). It’s a short way of saying "execute this.form.submit() if this.value is different from 'nAtualiza' .

Browser other questions tagged

You are not signed in. Login or sign up in order to post.