I've been using Bootstrap-3 with modals for the past couple of months. And it's the first time I've experience this problem. Since I want to show content when an option in a select is changed, I'm using the change function that Jquery provides. The problem here is that when I'm using this code in the modal, it triggers fine, and it closes and I open it again, the value of the select multiplies the times I have openned and closes. Example, if the value is 1, in the first time, and I close it, when I open it again, and change it to 2, it goes like this with the console.log: 1 2 2 And so on.

I've tried using the off method when the modal closes, but when I open the modal again the on method doesn't do anything.

$(".edit_data").on('click', function(){    
    var employee_id = $(this).attr("id"); 
  var periodo = null; 
  $('#evaluacionForm')[0].reset();
  $('#evaluadoss').html('');

    $.ajax({  
        url:"url",  
        method:"POST",  
        data:{employee_id:employee_id},  
        dataType:"json",  
        success:function(data){                        
            $('#id_usuario').val(data.id_usuario);  
            $('#nombre').val(data.usuario);
      var usuario = data.id_usuario;


      $("select[name=periodo_e]").change(function(){
        periodo = $('select[name=periodo_e]').val();   
        console.log(periodo);       
      $.ajax({                
        url: "url.php?id="+usuario+"&periodo="+periodo
        }).done(function(data) { // data what is sent back by the php page
        $('#evaluadoss').html(data); // display data
      });
                });
            $('#myModalEvaluacion').modal('show');  
        }  
    });        
});

$('#myModalEvaluacion').on('hidden.bs.modal', function () {
  $(".edit_data").off();

})

The problem persist when I'm doing the select option. It multiplies the values and goes crazy with the result that I want.

I expect the code not to multiply the result, since I'm opening and closing the modal, the id of the employee won't coincide with the employee id that displays in the change method.

0 Answers