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

168
Views
Cómo obtener la acción del formulario al enviar cuando se usa el atributo de formación en un botón

Un <form> tiene diferentes botones. En el evento de envío, me gustaría saber en qué botón se hizo clic. Tuve la idea de que el atributo de formaction en el botón podría usarse para esto. En MDN dice que el atributo de formaction :

Anula el atributo de acción del propietario del formulario del botón.

Pero al obtener la acción de formulario, este valor no cambia según la formaction . Entonces, ¿cómo puedo obtener el valor de la formaction o tiene alguna otra sugerencia para resolver esto?

 document.forms.form01.addEventListener('submit', e => { e.preventDefault(); console.log(e.target.action); switch (e.target.action) { case 'a': // do "a" stuff break; case 'b': // do "b" stuff break; } });
 <form name="form01" action="default"> <button formaction="a">A</button> <button formaction="b">B</button> </form>

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Echa un vistazo a e.submitter . le dará el elemento que envió el formulario

about 4 years ago · Juan Pablo Isaza Report

0

Agregue value al button y luego use el submitter como se muestra a continuación.

 document.forms.form01.addEventListener('submit', e => { e.preventDefault(); console.log(e.submitter.value); switch (e.submitter.value) { case 'a': // do "a" stuff break; case 'b': // do "b" stuff break; } });
 <form name="form01" action="default"> <button value="a">A</button> <button value="b">B</button> </form>

about 4 years ago · Juan Pablo Isaza Report

0

Si se adjunta una formaction al botón para anular el evento de envío, entonces debe tomar el elemento que tiene el enfoque actual después de enviar el evento de envío, por lo tanto, para tomar el valor del elemento activo, use document.activeElement

 document.forms.form01.addEventListener('submit', e => { e.preventDefault(); let action = document.activeElement.getAttribute("formaction"); console.log(action); switch (action) { case 'a': // do "a" stuff break; case 'b': // do "b" stuff break; } });
 <form name="form01" action="default" method="get"> <button formaction="a">A</button> <button formaction="b">B</button> </form>

about 4 years ago · Juan Pablo Isaza 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!