Update only the Divi I added via jquery

Asked

Viewed 23 times

0

Good night. I am adding dynamic Ivs via jquery, and they have a select that is already selected according to the patient’s registration, but every time I add a new div all the other ones are affected, that is, the selects of the other ones change the value.

Code:

    function getAllPlanos()
    {
      $.ajax({
        url:base_url+'/pacientes/getAllPlanos',
        method:"get",
        dataType:"json",
        success:function(data)
        { 
          var selected = '';
          $.each(data, function(i)
          { 
            if(paciente_plano === data[i].idPlano)
            {
              selected = 'selected';
            }
            $('select.orcamento_plano').append('<option '+selected+' value="'+data[i].idPlano+'">'+data[i].plano_nome+'</option>');
          });

          $('.orcamento_id_plano').val(paciente_plano);  
          $('select.orcamento_plano').selectpicker('refresh');
        }
      }); 
    }

    var html_novo_tratamento = '';
    html_novo_tratamento +=' <div class="row align-items-center mt-3 dinamic">';
    html_novo_tratamento +=' <div class="col-md-3 mt-3">';
    html_novo_tratamento +=' <div class="form-group bmd-form-group">';
    html_novo_tratamento +=' <label  class="bmd-label-static">Plano</label>';
    html_novo_tratamento +=' <select class="selectpicker orcamento_plano" data-live-search="true" data-width="100%" name="orcamento_plano[]" data-style="select-with-transition"  title="Plano" data-size="8" tabindex="-98">';
    html_novo_tratamento +=' </select>';
    html_novo_tratamento +=' <input type="hidden" class="orcamento_id_plano">';
    html_novo_tratamento +=' </div>';
    html_novo_tratamento +=' </div>';
    html_novo_tratamento +=' <div class="col-md-3 mt-3">';
    html_novo_tratamento +=' <div class="form-group bmd-form-group">';
    html_novo_tratamento +=' <label class="bmd-label-static">Tratamento</label>';
    html_novo_tratamento +=' <select class="selectpicker orcamento_tratameno" data-live-search="true" data-width="100%" name="orcamento_tratameno[]" data-style="select-with-transition"  title="Tratamento" data-size="8" tabindex="-98">';
    html_novo_tratamento +=' </select>';        
    html_novo_tratamento +=' </div>';
    html_novo_tratamento +=' </div>';
    html_novo_tratamento +=' <div class="col-md-2 mt-3">';
    html_novo_tratamento +=' <div class="form-group bmd-form-group">';
    html_novo_tratamento +=' <label  class="bmd-label-static">Dentes/Região</label>';
    html_novo_tratamento +=' <input type="text" class="form-control orcamento_tratamento_dente" name="orcamento_tratamento_dente[]" required="true" aria-required="true" aria-invalid="true">';
    html_novo_tratamento +=' </div>';
    html_novo_tratamento +=' </div>';
    html_novo_tratamento +=' <div class="col-md-2 mt-3">';
    html_novo_tratamento +=' <div class="form-group bmd-form-group">';
    html_novo_tratamento +=' <label class="bmd-label-static">Valor</label>';
    html_novo_tratamento +=' <input type="text" class="form-control orcamento_tratamento_valor" name="orcamento_tratamento_valor[]" required="true" aria-required="true" aria-invalid="true">';
    html_novo_tratamento +=' </div>';
    html_novo_tratamento +=' </div>';
    html_novo_tratamento +=' <div class="col-md-2 mt-3 text-right">';
    html_novo_tratamento +=' <div class="form-group bmd-form-group">';
    html_novo_tratamento +=' <div class="div-actions" style="display: block; margin-left: 1em;">';
    html_novo_tratamento +=' <a href="#" rel="tooltip" class="btn btn-dark btn-link remove">';
    html_novo_tratamento +=' <i class="material-icons" style="font-size: 2em">delete</i>';
    html_novo_tratamento +=' </a>';                                                           
    html_novo_tratamento +=' </div>';
    html_novo_tratamento +=' </div>';
    html_novo_tratamento +=' </div>';
    html_novo_tratamento +=' <div class="col-md-5 mt-3">';
    html_novo_tratamento +=' <div class="form-group bmd-form-group">';
    html_novo_tratamento +=' <label  class="bmd-label-static">Dentista</label>';
    html_novo_tratamento +=' <input type="text" class="form-control" name="orcamento_dentista[]" required="true" aria-required="true" aria-invalid="true">';
    html_novo_tratamento +=' </div>';
    html_novo_tratamento +=' </div>';                        
    html_novo_tratamento +=' </div>';

    $('.btn-novo-tratamento').on('click', function()
    { 
      $('.novo_tratamento').append(html_novo_tratamento);
      getAllPlanos();
    });

Another thing also, when I add a new div, is called the function that brings the plans, then in the first div the selects increase, in case pull twice. If I take the function select does not work in the added div.

  • 1

    I think you have to get the last one added. Try with $('select.orcamento_plano:last').

  • Right friend. Right

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.