jquery - Why is this JavaScript if statement always returning true? -


I am trying to execute a block of code when a div is first clicked, and after that A separate block is clicked a second time My alert shows that the variable is being changed to the first block, but the first code block always executes.

If I change the variable manually, then I can get it to execute another block.

  var clickcount = 0; // Click the first click (clickcount === 0) {$ ('.link'). Click (function () {warning ('first click, click =' +); clickcount = 1;}); } // If the code for the second click (clickcount === 1) {$ ('.link'). Click (function () {warning ('first click, clickcount =' + clickcount); //}); }  

Your code says:

  Variable attaches a click handler to 0 link that ignites the link to click on the link  

That click handler resides there, even after the variable has changed The status is evaluated only once and the click handler is only linked once, but every time the click is done, click on the link, then attach the attachment It is necessary to keep running.

The second block is never executed, as long as you re-run this entire block of code, which adds the click handlers.

You probably want to attach a click handler once, and inside that handler you check the value of clickcount and make something different depending on its value.

  var clickcount = 0; $ ('.link'). Click (function () {warning ('clickcount =' + clickcount); if (click ====) {clickcount = 1;} else {// stuff}});  

Comments