Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

110
Vistas
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 Respuestas
Responde la pregunta

0

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

about 4 years ago · Juan Pablo Isaza Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda