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

508
Views
Interceptar el evento de clic en un botón, pedir confirmación y continuar

Basado en una variable SomeCondition , necesito interceptar un evento de clic en un botón y pedir confirmación, si dice que está bien, continúe; de lo contrario, ignore el clic.

Entonces algo como:

 if(SomeCondition) { // disable click on button var isOk = window.confirm("Are you sure?"); if(isOk) { $("#button1").click(); } }

Nota: button1 ya se conectó con un evento de clic a través de javascript desde un archivo .js externo que no puedo cambiar.

No sé a qué se vinculó el evento de clic, así que tengo que deshabilitar el clic si SomeCondition es verdadero, luego solicito confirmación y luego continúo con el clic.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Procese isOK your window.confirm dentro de la función del botón

 $('#button1').click(function(){ if(window.confirm("Are you sure?")) alert('Your action here'); });

El problema que vas a tener es que el clic ya se ha producido cuando activas tu pregunta "¿Estás seguro?" Llamar a preventDefault no detiene la ejecución del clic original si es el que abrió tu ventana original.

Un pequeño problema con el huevo y la gallina.

Editar: después de leer su pregunta editada:

 var myClick = null; //get a list of jQuery handlers bound to the click event var jQueryHandlers = $('#button1').data('events').click; //grab the first jquery function bound to this event $.each(jQueryHandlers,function(i,f) { myClick = f.handler; return false; }); //unbind the original $('#button1').unbind('click'); //bind the modified one $('#button1').click(function(){ if(window.confirm("Are You Sure?")){ myClick(); } else { return false; } });
over 4 years ago · Santiago Trujillo Report

0

Con jQuery para evitar el valor predeterminado, simplemente return false :

 $("#button1").click(function () { //do your thing return false; });
over 4 years ago · Santiago Trujillo Report

0

Para cualquiera que esté buscando una solución simple (no particular a la condición especial mencionada aquí)

Simplemente agregue este atributo al botón:

 .... , onclick = "return confirm('are you sure?')"

Cuando el cuadro de confirmación devuelve un falso, el evento de clic se cancela. Nota: El controlador onclick mencionado explícitamente en línea anulará cualquier otro enlace.

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!