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

110
Views
el cambio de la identificación del botón conduce a la activación del evento de clic

Intento hacer un evento de clic con jquery.

El resultado que quiero se muestra a continuación:

primer clic - más uno

segundo clic - más uno

tercer clic - más uno y cambio de identificación y texto del botón

cuarto clic en adelante - más uno y ventana emergente de alerta

Sin embargo, la alerta aparece con el tercer clic.

mi html:

 <h1 id="number">0</h1> <button id="next">Add 1</button>

mi jquery:

 $(document).ready(function () { let num = 0; $("#next").click(function () { ++num; if (num > 2) { $("#next").html("Alert"); $("#next").attr('id', 'alert'); $("#number").html(num); } else { $("#number").html(num); } }); $(document).on("click", "#alert", function () { alert(num); }); });

No sé dónde me equivoqué, en teoría me parece bien, pero soy nuevo en jquery y debo haberme perdido algo.

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

0

Puedes probar el siguiente código:

 $(document).ready(function () { let num = 0; $(document).on("click", "#next", function () { num++; if (num > 2) { $("#next").html("Alert"); $("#next").attr('id', 'alert'); $("#number").html(num); } else { $("#number").html(num); } }); $(document).on("click", "#alert", function () { num++; $("#number").html(num); alert(num); }); });

Espero que funcione.

about 4 years ago · Juan Pablo Isaza Report

0

En el tercer clic

  • llame a e.stopPropagation() para evitar que el evento #next click se propague al elemento #alert .
  • desvincule el evento #next click usando $("#next").unbind("click"); o bien $("#next").click se activará el clic al hacer clic en #alert

Además, no olvide manejar la alerta y la lógica de incremento dentro del evento de clic "#alert" .

violín de trabajo

 $(document).ready(function () { let num = 0; $("#next").click(function (e) { ++num; if (num > 2) { $("#next").unbind("click"); $("#next").html("Alert"); $("#next").attr('id', 'alert'); $("#number").html(num); e.stopPropagation(); } else { $("#number").html(num); } }); $(document).on("click", "#alert", function () { ++num; alert(num); $("#number").html(num); }); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <h1 id="number">0</h1> <button id="next">Add 1</button>

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!