Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

1K
Views
Evite el envío de formularios al presionar Intro desde una entrada de texto, usando Vue.js

Estoy usando Vue.js en mi aplicación y tengo una entrada de texto dentro de un formulario

 <div id="myVueForm"> <form> <input type="text" v-on="keyup:addCategory | key 'enter'"> <!-- more form fields --> <button type="submit">Submit Form</button> </form> </div>

En mi instancia de Vue tengo lo siguiente

 new Vue({ el: '#myVueForm', methods: { addCategory: function(e) { if(e) e.preventDefault(); console.log("Added a new category, thanks!"); } } });

A pesar de la llamada a preventDefault() , cuando un usuario presiona enter mientras está ingresando texto, el formulario aún se envía (aunque el método addCategory() se activa). Este comportamiento se puede demostrar en este violín .

Sé que puedo usar jQuery para capturar el evento y evitar el envío, pero me gustaría ver si es posible hacer esto en Vue, ya que parece un uso bastante común.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Aquí hay una solución para Vue.js 2.x:

 <input type='text' v-on:keydown.enter.prevent='addCategory' />
over 4 years ago · Santiago Trujillo Report

0

El envío siempre se activa al pulsar el teclado. Entonces use keydown en lugar de keyup.

 <input type="text" v-on="keydown:addCategory | key 'enter'">
over 4 years ago · Santiago Trujillo Report

0

¿Por qué no deshabilitar el envío del formulario?

 <form v-on:submit.prevent><input .../></form>
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!