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:
<?php
require_once('session.php');
require_once('config.php');
?>
<?php
if (empty($_SESSION['nick']) || empty($_SESSION['haslo'])) {
}
else
{
$pokaz = mysql_query("SELECT * FROM nazwa_asortymentu LIMIT 0 , 30")
or
die('Blad zapytania'); }
?>
<html>
<head>
<title>
<?php echo $title; ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('a[id^="add-"]').click(function()
{
var i = $('#elements tr').length; // zliczanie wierszy
var x = i+1;
var id = extractLinkId($(this).attr('id'));
var nRow = $('#element-'+id);
var row = $(nRow).clone(); // klonowanie wiersza, który został klikniety a poniżej zmieniamy wartości nowego wiersza
row.filter('tr[name="element-'+id+'"]').attr('id', 'element-'+x);
row.filter('tr[id="element-'+x+'"]').attr('name', 'element-'+x);
row.find('td[name="lp['+id+']"]').attr('id', 'lp['+x+']');
row.find('td[id="lp['+x+']"]').attr('name', 'lp['+x+']').text(x);
row.find('td[name="index['+id+']"]').attr('id', 'index['+x+']');
row.find('td[id="index['+x+']"]').attr('name', 'index['+x+']');
row.find('td[name="nazwa['+id+']"]').attr('id', 'nazwa['+x+']');
row.find('td[id="nazwa['+x+']"]').attr('name', 'nazwa['+x+']');
row.find('input[name="partia['+id+']"]').attr('id', 'partia['+x+']');
row.find('input[id="partia['+x+']"]').attr('name', 'partia['+x+']');
row.find('input[name="ilosc['+id+']"]').attr('id', 'ilosc['+x+']');
row.find('input[id="ilosc['+x+']"]').attr('name', 'ilosc['+x+']')
row.find('select[name="forma['+id+']"]').attr('id', 'forma['+x+']');
row.find('select[id="forma['+x+']"]').attr('name', 'forma['+x+']');
row.find('a[id^="add-'+id+'"]').attr('id', 'usun-'+x);
row.find('a[id^="usun-'+x+'"]').attr('href', '#usun['+x+']');
row.find('a[id^="usun-'+x+'"] img').attr('src', 'images/del.png');
row.find('input').val('');
row.insertAfter(nRow); // dodajemy nowy wiersz
return false;
});
$('a[id^="usun-"]').live('click',function()
{
$(this).parents("tr").remove(); // usuwany wiersz
return false;
});
function extractLinkId(linkId)
{
return parseInt(linkId.substr('add-'.length));
}
});
</script>
</head>
<body>
<form action="parser.php" method="post">
<?php
$lp=0;
<table id="tabela">
<thead>
<tr>
<th>LP.</th>
<th>INDEX</th>
<th>NAZWA ASORTYMENTU</th>
<th>CYKL</th>
<th>ILOSC</th>
<th>FORMA</th>
<th>+/-</th>
</tr>
</thead>
';
<tbody id="elements">
';
{
$lp++;
<tr name="element-'.$lp.'" id="element-'.$lp.'">
<td id="lp['.$lp.']" name="lp['.$lp.']">'.$lp.'</td>
<td id="index['.$lp.']" name="index['.$lp.']">'.$wiersz['index'].'</td>
<td id="nazwa['.$lp.']" name="nazwa['.$lp.']">'.$wiersz['nazwa'].'</td>
<td><input type="text" name="partia['.$lp.']" id="partia['.$lp.']"/></td>
<td><input type="text" name="ilosc['.$lp.']" id="ilosc['.$lp.']"/></td>
<td><select name="forma['.$lp.']">
<option>*1/2</option>
<option>*1/4</option>
<option>*vac</option>
<option>*map</option>
<option>*porcje</option>
<option>*(066)</option>
<option>*(056)</option>
<option>*(057)</option>
<option>*(a-100g)</option>
<option>*(a-120g)</option>
<option selected="selected">*luz</option>
</select></td>
<td><a href="#add['.$lp.']" id="add-'.$lp.'"><img src="images/add.png"></a></td>
</tr>
';}
?>
</form>
</body>
</html>
Z góry dzięki za pomoc.
Poradziłem sobie już z tym.
Dodałem taką funkcję:
$('#send').live('click', function(){
$
('#tabela > tbody > tr').each(function(i
) {
$(this).attr('id', 'element-'+(i+1));
$(this).attr('name', 'element-'+(i+1));
$(this).find('td:first-child').text(i+1);
$(this).find('input:first-child').attr('id', 'partia['+(i+1)+']');
$(this).find('input:first-child').attr('name', 'partia['+(i+1)+']');
$(this).find('input:last-child').attr('id', 'ilosc['+(i+1)+']');
$(this).find('input:last-child').attr('name', 'ilosc['+(i+1)+']');
$(this).find('select:first-child').attr('id', 'forma['+(i+1)+']');
$(this).find('select:first-child').attr('name', 'forma['+(i+1)+']');
$(this).find('a[id^="add-"]:first-child').attr('href', '#add-'+(i+1));
$(this).find('a[id^="add-"]:first-child').attr('id', 'add-'+(i+1));
$(this).find('a[id^="usun-"]:first-child').attr('href', '#usun-'+(i+1));
$(this).find('a[id^="usun-"]:first-child').attr('id', 'usun-'+(i+1));
});
return false;
});
Wszystko jest ok.