Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

102
Visualizações
How can add a button to to search bar and keep the keypress - VUE

My search is working, but I need to press enter key to have results. I want to add the option of having results with a button too. Search for results by pressing enter or clinking button. Can anyone help me?

<div class="search-box">
    <input 
      type="text" 
      class="search-bar" 
      placeholder="Search..."
      v-model="query"
      @keypress="fetchWeather"
    />
   <button>Click Me!</button>
  </div>

    methods: {
        fetchWeather (e) {
          if (e.key == "Enter") {
            fetch(`${this.url_base}weather?q=${this.query}&units=metric&APPID=${this.api_key}`)
              .then(res => {
                return res.json();
              }).then(this.setResults);
          }
        },
        setResults (results) {
          this.weather = results;
        }
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You can use @keyup.enter="fetchWeather" for input, and @click="fetchWeather" for button.

about 4 years ago · Juan Pablo Isaza Relatório

0

It should be working fine if you just add @click="fetchWeather" inside the button tag, like this:

<button @click="fetchWeather">Click me"

about 4 years ago · Juan Pablo Isaza Relatório

0

Try this :

new Vue({
  el: '#app',
  data: {
    query: null,
    queryData: ['search1', 'search2', 'search3', 'search4'],
    searchedData: []
  },
  mounted() {
    this.searchedData = [...this.queryData]
  },
  methods: {
    fetchWeather() {
      if (!this.query) return;
      this.searchedData = this.queryData.filter(item => item.includes(this.query))
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <input 
         type="text" 
         class="search-bar" 
         placeholder="Search..."
         v-model="query"
         @keyup.enter="fetchWeather"
         />
  <button @click="fetchWeather">Click Me!</button>
  
  <ul>
    <li v-for="(item, index) in searchedData" :key="index"> {{ item }} </li>
  </ul>
</div>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda