I can't figure out what's happening here. When I select the $('#semestre')
option, everything works perfectly. Then I select the $('#turma')
option and change its value. Everything works. Then, I change the $('#semestre')
value again, and everything works. However, when I change again the $('#turma')
value, I get the alert twice. And if I do it again, I get three times the alert and so on. What am I missing here?
$(document).ready(function(){
$('.turma').hide();
$('#semestre').change(function(){
if($(this).val() != ''){
$('.options_turma').remove();
$('.turma').show();
$.post('seleciona_turmas.php', {cd_turma: $(this).val()}, function(data){
var item;
data = jQuery.parseJSON(data);
$.each(data.json, function(){
item = "<option class='options_turma' value='" + this['codigo'] + "'>" + this['descricao'] + "</option>";
$('#turma').append(item);
});
$('#turma').change(function(){
if($(this).val() != ''){
alert("OK");
}else{
alert('NULL');
}
});
});
}else{
$('#turma').val('');
$('.turma').hide();
}
});
});
I tried to be clear, but it's a confusing question.