Task setting . The table must be equipped with the ability to delete certain lines from it. Click on the word delete in the line -> a modal menu opens, where the user is asked again what exactly to delete or to cancel. Clicking on the button of the modal window "Delete" from the table should disappear the corresponding row.

Markup

There is a markup c table. Two columns, the first with an identifier, and the second with some text. The first line is the header, and the left ones are normal lines:

<table id="table_main"> <tr> <th>id</th> <th>ВСкст Π·Π°Π΄Π°Ρ‡ΠΈ</th> <th>ДСйствия</th> </tr> <tr> <td>1</td> <td>ВСкст 1</td> <td><p class="delete">Π£Π΄Π°Π»ΠΈΡ‚ΡŒ</p></td> </tr> <tr> <td>2</td> <td>ВСкст 2</td> <td><p class="delete">Π£Π΄Π°Π»ΠΈΡ‚ΡŒ</p></td> </tr> <tr> <td>3</td> <td>ВСкст 3</td> <td><p class="delete">Π£Π΄Π°Π»ΠΈΡ‚ΡŒ</p></td> </tr> <tr> <td>4</td> <td>ВСкст 4</td> <td><p class="delete">Π£Π΄Π°Π»ΠΈΡ‚ΡŒ</p></td> </tr> <tr> <td>5</td> <td>ВСкст 5</td> <td><p class="delete">Π£Π΄Π°Π»ΠΈΡ‚ΡŒ</p></td> </tr> <tr> <td>6</td> <td>ВСкст 6</td> <td><p class="delete">Π£Π΄Π°Π»ΠΈΡ‚ΡŒ</p></td> </tr> </table> 

And the modal window:

  <div class="modal_delete"> <div id="modal_button_container"> <button id="button_delete">Π£Π΄Π°Π»ΠΈΡ‚ΡŒ</button> <button id="button_cancel">ΠžΡ‚ΠΌΠ΅Π½Π°</button> </div> </div> 

My script

 /*Π‘ΠΊΡ€ΠΈΠΏΡ‚ удалСния Π·Π°Π΄Π°Ρ‡ΠΈ*/ $(".delete").click(function () { // ΠžΠΏΡ€Π΅Π΄Π΅Π»ΡΠ΅ΠΌ Π½ΠΎΠΌΠ΅Ρ€ строки ΠΏΠΎ Π½Π°ΠΆΠ°Ρ‚ΠΎΠΌΡƒ p var num = $(this).parent().parent().index(); // Π”Π΅Π»Π°Π΅ΠΌ модальноС ΠΎΠΊΠ½ΠΎ Π²ΠΈΠ΄ΠΈΠΌΡ‹ΠΌ $(".modal_delete").css("display", "flex"); // Кнопка удалСния $("#button_delete").click(function () { // Π‘ΠΊΡ€Ρ‹Π²Π°Π΅ΠΌ модальноС ΠΎΠΊΠ½ΠΎ $(".modal_delete").hide(); // УдаляСм строку $("#table_main").children().children().eq(num).remove(); // (1) }); }); 

What is the problem ?

The first time the function works fine. But with each subsequent call it deletes several lines already. Through the browser console revealed that the line of code (*) works several times: the first time it is called once, the second time - twice, the third time - three times, etc. You can see that the value of the variable num changes every time.

For example, if I click on "Delete" lines 1 - 3 - 5, then the second call to the num function will be 3 when the line (1) is triggered with num 3, then the value of num will change to the previous one - 1, and the line (1) will work again. In the third call, the values ​​of num will change in this order: 5 - 3 - 1 (that is, the reverse).

Why does num change its meaning? I see this behavior of variables for the first time.

Question : how to ensure the operation of the string (*) once? How to make the num value the only one?

PS Alternative solutions to the problem are also being taken.

    1 answer 1

    The fact is that you hung the listener every time the "delete" button was pressed (not in the modal)

    slightly corrected your code so that it would become working

     var num = null $(".delete").click(function () { // ΠžΠΏΡ€Π΅Π΄Π΅Π»ΡΠ΅ΠΌ Π½ΠΎΠΌΠ΅Ρ€ строки ΠΏΠΎ Π½Π°ΠΆΠ°Ρ‚ΠΎΠΌΡƒ p num = $(this).parent().parent().index(); // Π”Π΅Π»Π°Π΅ΠΌ модальноС ΠΎΠΊΠ½ΠΎ Π²ΠΈΠ΄ΠΈΠΌΡ‹ΠΌ $(".modal_delete").css("display", "flex"); }); // Кнопка удалСния $("#button_delete").click(function () { // Π‘ΠΊΡ€Ρ‹Π²Π°Π΅ΠΌ модальноС ΠΎΠΊΠ½ΠΎ $(".modal_delete").hide(); // УдаляСм строку $("#table_main").children().children().eq(num).remove(); // (1) }); 
    • Here it is! Thank you - Kniga