Witam.
Potrzebuje Waszej pomocy.
Mianowicie chodzi o to, że zrobiłem skrypt wyświetlający jakieś dane z bazy danych i mam skrypt jquery dzięki któremu klonuje wskazany wiersz i wstawiam go za klonowanym. Gdy sklonuje pierwszy wiersz kilka razy, później sklonuje wiersz trzeci kilka razy a następnie usunę wszystkie nowo utworzone wiersze na bazie pierwszego i sklonuje kilka razy wiersz drugi to powtarzają mi się id wiersza. Macie jakieś pomysły jak to rozwiązać?
Mój kod:
  1. <?php
  2. require_once('session.php');
  3. require_once('config.php');
  4. ?>
  5. <?php
  6. if (empty($_SESSION['nick']) || empty($_SESSION['haslo']))
  7. {
  8. header('location index.php');
  9. exit ();
  10. }
  11. else
  12. {
  13. $pokaz = mysql_query("SELECT * FROM nazwa_asortymentu LIMIT 0 , 30")
  14.  
  15. or die('Blad zapytania');
  16. }
  17.  
  18. ?>
  19. <html>
  20. <head>
  21. <title><?php echo $title; ?></title>
  22. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  23. <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
  24. <script type="text/javascript" src="jquery.js"></script>
  25. <script type="text/javascript">
  26. $(document).ready(function() {
  27. $('a[id^="add-"]').click(function()
  28. {
  29. var i = $('#elements tr').length; // zliczanie wierszy
  30. var x = i+1;
  31. var id = extractLinkId($(this).attr('id'));
  32. var nRow = $('#element-'+id);
  33. var row = $(nRow).clone(); // klonowanie wiersza, który został klikniety a poniżej zmieniamy wartości nowego wiersza
  34. row.filter('tr[name="element-'+id+'"]').attr('id', 'element-'+x);
  35. row.filter('tr[id="element-'+x+'"]').attr('name', 'element-'+x);
  36. row.find('td[name="lp['+id+']"]').attr('id', 'lp['+x+']');
  37. row.find('td[id="lp['+x+']"]').attr('name', 'lp['+x+']').text(x);
  38. row.find('td[name="index['+id+']"]').attr('id', 'index['+x+']');
  39. row.find('td[id="index['+x+']"]').attr('name', 'index['+x+']');
  40. row.find('td[name="nazwa['+id+']"]').attr('id', 'nazwa['+x+']');
  41. row.find('td[id="nazwa['+x+']"]').attr('name', 'nazwa['+x+']');
  42. row.find('input[name="partia['+id+']"]').attr('id', 'partia['+x+']');
  43. row.find('input[id="partia['+x+']"]').attr('name', 'partia['+x+']');
  44. row.find('input[name="ilosc['+id+']"]').attr('id', 'ilosc['+x+']');
  45. row.find('input[id="ilosc['+x+']"]').attr('name', 'ilosc['+x+']')
  46. row.find('select[name="forma['+id+']"]').attr('id', 'forma['+x+']');
  47. row.find('select[id="forma['+x+']"]').attr('name', 'forma['+x+']');
  48. row.find('a[id^="add-'+id+'"]').attr('id', 'usun-'+x);
  49. row.find('a[id^="usun-'+x+'"]').attr('href', '#usun['+x+']');
  50. row.find('a[id^="usun-'+x+'"] img').attr('src', 'images/del.png');
  51. row.find('input').val('');
  52.  
  53. row.insertAfter(nRow); // dodajemy nowy wiersz
  54.  
  55. return false;
  56. });
  57. $('a[id^="usun-"]').live('click',function()
  58. {
  59. $(this).parents("tr").remove(); // usuwany wiersz
  60. return false;
  61. });
  62. function extractLinkId(linkId)
  63. {
  64. return parseInt(linkId.substr('add-'.length));
  65. }
  66. });
  67. </script>
  68. </head>
  69. <body>
  70. <form action="parser.php" method="post">
  71. <?php
  72. $lp=0;
  73. echo '
  74. <table id="tabela">
  75. <thead>
  76. <tr>
  77. <th>LP.</th>
  78. <th>INDEX</th>
  79. <th>NAZWA ASORTYMENTU</th>
  80. <th>CYKL</th>
  81. <th>ILOSC</th>
  82. <th>FORMA</th>
  83. <th>+/-</th>
  84. </tr>
  85. </thead>
  86. ';
  87. echo '
  88. <tbody id="elements">
  89. ';
  90. while ($wiersz = mysql_fetch_array($pokaz))
  91. {
  92. $lp++;
  93. echo '
  94. <tr name="element-'.$lp.'" id="element-'.$lp.'">
  95. <td id="lp['.$lp.']" name="lp['.$lp.']">'.$lp.'</td>
  96. <td id="index['.$lp.']" name="index['.$lp.']">'.$wiersz['index'].'</td>
  97. <td id="nazwa['.$lp.']" name="nazwa['.$lp.']">'.$wiersz['nazwa'].'</td>
  98. <td><input type="text" name="partia['.$lp.']" id="partia['.$lp.']"/></td>
  99. <td><input type="text" name="ilosc['.$lp.']" id="ilosc['.$lp.']"/></td>
  100. <td><select name="forma['.$lp.']">
  101. <option>*1/2</option>
  102. <option>*1/4</option>
  103. <option>*vac</option>
  104. <option>*map</option>
  105. <option>*porcje</option>
  106. <option>*(066)</option>
  107. <option>*(056)</option>
  108. <option>*(057)</option>
  109. <option>*(a-100g)</option>
  110. <option>*(a-120g)</option>
  111. <option selected="selected">*luz</option>
  112. </select></td>
  113. <td><a href="#add['.$lp.']" id="add-'.$lp.'"><img src="images/add.png"></a></td>
  114. </tr>
  115. ';}
  116. echo '</tbody></table>';
  117. ?>
  118. </form>
  119. </body>
  120. </html>

Z góry dzięki za pomoc.

Poradziłem sobie już z tym.
Dodałem taką funkcję:
  1. $('#send').live('click', function(){
  2. $('#tabela > tbody > tr').each(function(i)
  3. {
  4. $(this).attr('id', 'element-'+(i+1));
  5. $(this).attr('name', 'element-'+(i+1));
  6. $(this).find('td:first-child').text(i+1);
  7. $(this).find('input:first-child').attr('id', 'partia['+(i+1)+']');
  8. $(this).find('input:first-child').attr('name', 'partia['+(i+1)+']');
  9. $(this).find('input:last-child').attr('id', 'ilosc['+(i+1)+']');
  10. $(this).find('input:last-child').attr('name', 'ilosc['+(i+1)+']');
  11. $(this).find('select:first-child').attr('id', 'forma['+(i+1)+']');
  12. $(this).find('select:first-child').attr('name', 'forma['+(i+1)+']');
  13. $(this).find('a[id^="add-"]:first-child').attr('href', '#add-'+(i+1));
  14. $(this).find('a[id^="add-"]:first-child').attr('id', 'add-'+(i+1));
  15. $(this).find('a[id^="usun-"]:first-child').attr('href', '#usun-'+(i+1));
  16. $(this).find('a[id^="usun-"]:first-child').attr('id', 'usun-'+(i+1));
  17. });
  18. return false;
  19. });

Wszystko jest ok. smile.gif