Javascript confirmation dialog takes three clicks "cancel" to close

The following code is responsible for creating this unusual problem:

<script type="text/javascript"> $(document).ready(function () { $('.deleteRow').click(function (event) { event.preventDefault(); if (confirm('Delete?')) { var $t = $(this); $.post($(this).attr('href'), function (data) { if (data) { $t.parent().parent().remove(); } }); } return false; }); }); </script> 

Thanks in advance!

Here's the HTML:

 <td> <%= Html.ActionLink("<-Delete", "Delete", new {quoteID = quote.QuoteID}, new {@class= "deleteRow"}) %></td> <td> <a class="deleteRow" href="/Quote/Delete/56">&lt;-Delete</a></td> 

Here we are.

  <tbody> <tr> <td> <p> asd</p> &lt;div firebugversion=&quot;1.5.4&quot; id=&quot;_firebugConsole&quot; style=&quot;display: none;&quot;&gt; &amp;nbsp;&lt;/div&gt; &lt;br /&gt; </td> <td> 2345 </td> <td> 7/28/2010 3:26:10 PM </td> <td> <a class="deleteRow" href="/Quote/Delete/56">&lt;-Delete</a></td> <script type="text/javascript"> $(document).ready(function () { $('.deleteRow').click(function (event) { event.preventDefault(); event.stopPropagation(); if (confirm('Delete?')) { var $t = $(this); $.post($(this).attr('href'), function (data) { if (data) { $t.parent().parent().remove(); } }); } return false; }); }); </script> </tr> <tr> <td> Now is the time for all good men to come to the aid of their parties. </td> <td> </td> <td> 7/6/2010 10:13:44 PM </td> <td> <a class="deleteRow" href="/Quote/Delete/2">&lt;-Delete</a></td> <script type="text/javascript"> $(document).ready(function () { $('.deleteRow').click(function (event) { event.preventDefault(); event.stopPropagation(); if (confirm('Delete?')) { var $t = $(this); $.post($(this).attr('href'), function (data) { if (data) { $t.parent().parent().remove(); } }); } return false; }); }); </script> </tr> <tr> <td> I&#39;ma loser </td> <td> 146 </td> <td> 7/6/2010 9:11:42 PM </td> <td> <a class="deleteRow" href="/Quote/Delete/1">&lt;-Delete</a></td> <script type="text/javascript"> $(document).ready(function () { $('.deleteRow').click(function (event) { event.preventDefault(); event.stopPropagation(); if (confirm('Delete?')) { var $t = $(this); $.post($(this).attr('href'), function (data) { if (data) { $t.parent().parent().remove(); } }); } return false; }); }); </script> </tr> </tbody> 

Thanks for all the answers, by the way.

+4
source share
3 answers

Now that you have placed the generated HTML, just make sure you include the jQuery output inside the loop that generates your TD elements. It should go beyond this loop, preferably at the bottom of the page!

If you look at HTML, you will see this 3 times:

 <script type="text/javascript"> $(document).ready(function () { $('.deleteRow').click(function (event) { event.preventDefault(); event.stopPropagation(); if (confirm('Delete?')) { var $t = $(this); $.post($(this).attr('href'), function (data) { if (data) { $t.parent().parent().remove(); } }); } return false; }); }); </script> 

Now you could say, β€œYes, that was three times, but shouldn't it be executed once? In the end, I update the click handler!” Oh no. The jQuery.click () method associates a function with a specific event, adding this function to the eventListeners list for this event. Since event binding works in general in Javascript. Linking means adding it to the list.

If you want to make sure that the click handler you add is ONLY the click handler for the element, you will need to use unbind first:

  $('.deleteRow').unbind('click').click(function (event) { // rest of code... 

instead of this:

  $('.deleteRow').click(function (event) { // rest of code... 
+6
source

BINGO ~ you only need to call the ONCE middleware event!

you do it three times.

there should be only one script block at the bottom of the page

also:

when you do this:

 $t = $(this); 

this request is now stored in $ t so you can reuse it.

 $.post($(this).attr('href'), function (data) { 

can only use stored result set

 $.post($t.attr('href'), function (data) { 

Good luck.

+2
source

I made this test page using the information you provided. This behavior does not occur. (I added TRs, but it doesn’t matter - it worked fine before).

What is the difference between your test page and this example?

 <html> <head> <script src='http://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.min.js'> </script> <script type="text/javascript"> $(document).ready(function () { $('.deleteRow').click(function (event) { event.preventDefault(); if (confirm('Delete?')) { var $t = $(this); $.post($(this).attr('href'), function (data) { if (data) { $t.parent().parent().remove(); } }); } return false; }); }); </script> </head> <body> <table> <tr><td> <a class="deleteRow" href="/Quote/Delete/56">&lt;-Delete</a></td></tr> <tr><td> <a class="deleteRow" href="/Quote/Delete/56">&lt;-Delete</a></td></tr> <tr><td> <a class="deleteRow" href="/Quote/Delete/56">&lt;-Delete</a></td></tr> </table> </body> </html> 
+1
source

All Articles