I have the following HTML code:
<label id="meuTemplate" for="criaAssoc" class="instAssoc" style="display:none">
<strong>ID:</strong>
<input name="idInstrucao" type="text">
<strong>Ordem:</strong>
<input type="text">
<span>
<strong class="instTxt">Instrução:</strong>
<textarea disabled="disabled"></textarea>
</span>
<img src="images/1.png" title="Remover instrução" class="botaoExcluir" >
</label>
Which has the following result as in the print below:
ThroughthisJScode(below)usingjQuerywhenIclicktheplusbuttonIcanclonethelabelpassedintheHTMLabove.
//Scriptparaadicionarlabelsvarids=1;$(".btnNewInst").click(function(){
var novoId = "lb_" + (ids++);
var clone = $("#meuTemplate").clone();
clone.prop("id", novoId); // É necessário mudar o id depois que se clona
$(".instAssoc:last").after("<br>", clone);
clone.show();
clone.find(".botaoExcluir").click(function() {
clone
.prev().remove().end() // Remove também o <br>
.remove();
});
});
//Disparando evento click para criar a primeira label
$(".btnNewInst").click();
The result of cloning is this:
HoweverIuseaJScodeusingAjaxtoreadinformationfrommydatabase.But,Icannotdothislabelalabelcanonlysucceedonthefirstlabel.ThefollowingonesthatareclonedAjaxcannotrotate.AndevenifthelabelhasalreadybeenclonedbeforetheeventthattriggersAjaxistriggered,itonlyrunsonthefirstlabel.Theinformationthatwastocomeoutintheseconddoesnotcomeout,itonlymakesclonetheinformationofthefirstlabel.Hereisaprintofthesituationdescribed.
Below is the script that AJAX is in:
//Ajax para recuperar o texto da instrução passando o ID
$("input[name='idInstrucao']").focusout(function(){
$.ajax({
url: 'ajax/instrucaoAjax.php',
type: 'POST',
data: 'idInstrucao='+$(this).val(),
beforeSend: '',
error: function(leitura){
$("textarea").val(leitura);
},
success: function(leitura){
if(leitura == 1){
$("textarea").val("Esta pergunta não existe!");
}else{
$("textarea").val(leitura);
}
}
});
});
Any suggestions for this BUG?
Update:
I made the changes as suggested. But, the problem persists. Now it no longer replicates the result of the first label in the second. However, it does not work on the cloned label's. Look how my script was after the changes:
//Script para adicionar labels
var ids = 1;
$(".btnNewInst").click(function(){
var novoId = "lb_" + (ids++);
var clone = $("#meuTemplate").clone();
clone.prop("id", novoId); // É necessário mudar o id depois que se clona
$(".instAssoc:last").after("<br>", clone);
clone.show();
$(document).on('click', '.botaoExcluir', function() {
$(this).closest('label')
.prev().remove().end()
.remove();
});
});
//Disparando evento click para criar a primeira label
$(".btnNewInst").click();
//Ajax para recuperar o texto da instrução passando o ID
$("input[name='idInstrucao']").focusout(function(){
var valor = this.value;
var self = this;
var textArea= $(self).closest('label').find('textarea');
$.ajax({
url: 'ajax/instrucaoAjax.php',
type: 'POST',
data: 'idInstrucao='+valor,
beforeSend: '',
error: function(leitura){
alert(leitura);
},
success: function(leitura){
if(leitura == 1){
textArea.val("Esta pergunta não existe!");
}else{
textArea.val(leitura);
}
}
});
});