CODE RÉVISÉ À LA FINjQuery - Attacher un bouton de suppression au script de dépôt automatique?
Je suis très nouveau pour jquery, et même si je l'aime, il y a beaucoup, je dois encore apprendre ... Le code ci-dessous ajoutera une nouvelle ligne si l'utilisateur clique dans l'une des cellules existantes dans une rangée. Cette partie fonctionne bien. J'essaie de comprendre comment avoir un bouton [-] à la fin de chaque ligne sur lequel un utilisateur peut cliquer pour enlever cette ligne au cas où ils font une erreur? Est-ce que c'est possible?
Voici le jquery
$(function(){
$("#knotes > tbody > tr > td > input").bind('focus', function(){
var row = $(this).closest("tr").get(0);
if(row.className.indexOf("clicked")==-1)
{
var rowCopy=$(row).clone(true);
$(row).closest("tbody").append(rowCopy);
row.className+="clicked";
var newInput=$("input",rowCopy).get(0);
newInput.id="newId";
$(newInput).bind('focus',attachAutoCompleteEmployeeValues);
}
});
});
Voici le balisage
<table width="100%" cellpadding="0" cellspacing="0" id="knotes">
<thead bgcolor="#f7f9c9">
<td align="center"><label for="name">Name</label></td>
<td align="center" nowrap="nowrap"><label for="kot">OT </label></td>
<td> </td>
<td align="center" nowrap="nowrap"><label for="kdt">DT </label></td>
<td> </td>
<td align="center"><label for="kbreak">Bk?</label></td>
<td> </td>
<td align="center"><label for="kshift">Shift</label></td>
<td> </td>
</thead>
<tr>
<td align="center" class="kac" id="test"><input type="text" id="kemployee" name="klabor[kemployee][]" /></td>
<td align="center"><input type="text" name="klabor[kot][]" /></td>
<td> </td>
<td align="center"><input type="text" name="klabor[kdt][]" /></td>
<td> </td>
<td align="center"><input type="text" name="klabor[kbreak][]" /></td>
<td> </td>
<td align="center"><input type="text" name="klabor[kshift][]" /></td>
</tr>
</table>
VOICI LE CODE RÉVISÉ La version révisée jQuery
$(function(){
$("#knotes > tbody > tr > td > input").bind('focus', function(){
var row = $(this).closest("tr").get(0);
if(row.className.indexOf("clicked")==-1)
{
var rowCopy=$(row).clone(true);
$(row).closest("tbody").append(rowCopy);
row.className+="clicked";
var newInput=$("input",rowCopy).get(0);
newInput.id="newId";
$(newInput).bind('focus',attachAutoCompleteEmployeeValues);
$('minus').live(function(){$(this).closest('tr').remove();});
}
});
});
Le balisage révisé
<table width="100%" cellpadding="0" cellspacing="0" id="knotes">
<thead bgcolor="#f7f9c9">
<td align="center"><label for="name">Name</label></td>
<td align="center" nowrap="nowrap"><label for="kot">OT </label></td>
<td> </td>
<td align="center" nowrap="nowrap"><label for="kdt">DT </label></td>
<td> </td>
<td align="center"><label for="kbreak">Bk?</label></td>
<td> </td>
<td align="center"><label for="kshift">Shift</label></td>
<td> </td>
</thead>
<tr>
<td align="center" class="kac" id="test"><input type="text" id="kemployee" name="klabor[kemployee][]" /></td>
<td align="center"><input type="text" name="klabor[kot][]" value="" /></td>
<td> </td>
<td align="center"><input type="text" name="klabor[kdt][]" value="" /></td>
<td> </td>
<td align="center"><input type="text" name="klabor[kbreak][]" value="" /></td>
<td> </td>
<td align="center"><input type="text" name="klabor[kshift][]" value="" /></td><td class="minus"><img src="/images/minus.png" /></td>
</tr>
</table>
J'apprécie vraiment la clarification, mais quand je fais cela (exactement comme il est écrit), je reçois ceci: F est indéfini [Pause sur cette erreur] (function() {var R =/((?: \\ ((?: \\ ([^()] + \\) | [... (J, type de K === "chaîne "? K: K +" px ")}})})(); – phpN00b
Je suppose que attachAutoCompleteEmployeeValues est null – jantimon