Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [JavaScript]Problem z funkcją - Uncaught TypeError is not a function
matti9410
post 24.03.2020, 17:21:20
Post #1





Grupa: Zarejestrowani
Postów: 70
Pomógł: 0
Dołączył: 10.07.2013

Ostrzeżenie: (0%)
-----


Witam napisałem dwie funkcje do dodawania oraz usuwania div-a z kodu.

Kod
    function add_item(id) {
        var cnt_str = $("#edit_items_" + id + " button:last").val();
        
        if(typeof cnt_str === 'undefined'){
            var cnt_str = "0";
        }
        
        var cnt = parseInt(cnt_str, 10);
        ++cnt;
        
        var mySecondDiv = '<div class="form-row" id="item_' + id + "_" + cnt + '">';
        mySecondDiv = mySecondDiv +    '<div class="form-group col-md-7" id="item_select_' + cnt + '">';
        mySecondDiv = mySecondDiv +    '</div>';
        mySecondDiv = mySecondDiv +    '<div class="form-group col-md-4">';
        mySecondDiv = mySecondDiv +    '<input type="number" class="form-control" name="items_quantity[]" placeholder="Ilość" required>';
        mySecondDiv = mySecondDiv +    '</div>';
        mySecondDiv = mySecondDiv +    '<div class="form-group col-md-1">';
        mySecondDiv = mySecondDiv +    '<button type="button" class="btn btn-danger" onclick="delete_item(' + id + ', ' + cnt + ')" value="' + cnt + '" id="delete_item">Usuń</button>';
        mySecondDiv = mySecondDiv +    '</div>';
        mySecondDiv = mySecondDiv +    '</div>';
        $("#edit_items_" + id).append(mySecondDiv);
        $("#items_select").clone().appendTo("#item_select_" + cnt);
    };
    
    function delete_item(id, item) {
        $("#item_" + id + "_" + item).remove();
    };


Za pomocą Smarty generuje modal-e

  1. {foreach from=$data_services_table key=id item=n}
  2. <!-- The Modal -->
  3. <div class="modal fade" id="modal_edit_service_{$n.id}">
  4. <div class="modal-dialog modal-xl">
  5. <div class="modal-content">
  6.  
  7. <form enctype="multipart/form-data" action="../offers/" method="POST">
  8. <!-- Modal Header -->
  9. <div class="modal-header">
  10. <h3 class="modal-title">Dane usługi</h3>
  11. <button type="button" class="close" data-dismiss="modal">×</button>
  12. </div>
  13.  
  14. <!-- Modal body -->
  15. <div class="modal-body">
  16. <div class="form-row">
  17. <div class="form-group col-md-4">
  18. <label for="name_items">Nazwa</label>
  19. <input type="text" size="64" class="form-control" name="name" placeholder="Nazwa" value="{$n.name}" required>
  20. <input type="hidden" class="form-control" name="id" value="{$n.id}" required>
  21. </div>
  22. <div class="form-group col-md-4">
  23. <label for="name_items">Rok oferty</label>
  24. <input type="number" step="1" class="form-control" name="year_service" placeholder="Rok" value="{$n.year_service}" required>
  25. </div>
  26. </div>
  27. <div class="form-row">
  28. <div class="form-group col-md-12">
  29. <label for="price_items">Cena (PLN)</label>
  30. <div class="form-row">
  31. <div class="form-group col-md-5">
  32. <select class="form-control" name="price_option">
  33. <option value="0">Cena (PLN) obliczona z przedmiotów</option>
  34. <option value="1" selected>Cena (PLN) zadana</option>
  35. </select>
  36. </div>
  37. <div class="form-group col-md-7">
  38. <input type="number" step="0.01" class="form-control" name="price" placeholder="Cena (PLN)" value="{$n.price}">
  39. </div>
  40. </div>
  41. </div>
  42. </div>
  43. <div class="form-row">
  44. <div class="form-group col-md-12">
  45. <label for="nip">Opis</label>
  46. <input type="text" size="255" class="form-control" name="description" placeholder="Krótki opis" value="{$n.description}"required>
  47. </div>
  48. </div>
  49. <div class="form-row">
  50. <div class="form-group col-md-12">
  51. <label for="items">Przedmioty</label>
  52. <div id="edit_items_{$n.id}">
  53. {foreach from=$n.items_quantity key=id item=x name=items}
  54. <div class="form-row" id="item_{$n.id}_{$smarty.foreach.items.index}">
  55. <div class="form-group col-md-7">
  56. <select class="form-control" name="items_id[]" id="items_select">
  57. {foreach from=$data_items_table key=id item=i}
  58. {if $i.id eq $x.id}
  59. <option value="{$i.id}" selected>{$i.name} - {$i.price} </option>
  60. {else}
  61. <option value="{$i.id}">{$i.name} - {$i.price} PLN</option>
  62. {/if}
  63. {/foreach}
  64. </select>
  65. </div>
  66. <div class="form-group col-md-4">
  67. {foreach from=$data_items_table key=id item=i}
  68. {if $x.id eq $i.id}
  69. <input type="number" class="form-control" name="items_quantity[]" placeholder="Ilość" value="{$x.quantity}" required>
  70. {/if}
  71. {/foreach}
  72. </div>
  73. <div class="form-group col-md-1">
  74. {if $smarty.foreach.items.index > 0}
  75. <button type="button" class="btn btn-danger" onclick="delete_item({$n.id}, {$smarty.foreach.items.index})">Usuń</button>
  76. {/if}
  77. </div>
  78. </div>
  79. {/foreach}
  80. </div>
  81. </div>
  82. </div>
  83. <div class="form-row">
  84. <div class="form-group col-md-12">
  85. <div class="text-right">
  86. <div class="form-row">
  87. <div class="form-group col-md-12">
  88. <button type="button" class="btn btn-primary" onclick="add_item({$n.id})">Dodaj nowy przedmiot</button>
  89. </div>
  90. </div>
  91. </div>
  92. </div>
  93. </div>
  94. </div>
  95.  
  96. <!-- Modal footer -->
  97. <div class="modal-footer">
  98. <button type="submit" class="btn btn-primary" name="submit_save_service_information">Zapisz</button>
  99. <button type="button" class="btn btn-secondary" data-dismiss="modal">Zamknij</button>
  100. </div>
  101. </form>
  102. </div>
  103. </div>
  104. </div>
  105. {/foreach}


Gdy otwieram modal i posiadam wygenerowane przyciski "Usuń" wszystko działa poprawnie. Problem pojawia się gdy usune wszystkie div-y mające przycisk "Usuń" a następnie przyciskiem "Dodaj nowy przedmiot" dodam div-y i przyciskiem "Usuń" próbuję je usunąć, wtedy pojawia się w przeglądarce błąd.

Cytat
Uncaught TypeError: delete_item is not a function
at HTMLButtonElement.onclick ((index):1)


Ten post edytował matti9410 24.03.2020, 17:24:30
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 29.03.2024 - 00:11