Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

167
Vistas
How to solved always looping select option value from ajax

I have a data, when I update the data (using modal), select option work correctly enter image description here

When I close the modal, and I click the edit button again, something wrong with select option: enter image description here

This is my edit modal ajax:

// Function for edit modal plan schedule
    $('body').on('click', '.editPlanSchedule', function() {
        var Item_id = $(this).data('id');
        $.get("/quotation/getEditPlanSchedule" + '/' + Item_id, function(data) {
            console.log(data['product_plan']);
            $('.modal-title-edit').html("Edit Plan Schedule Item");
            $('#saveBtn').val("Update");
            $('#updatePlanSchedule').modal('show');
            $('#id').val(data['data'].id);
            $('#qno').val(data['data'].qno);
            $('#b_amount').val(data['data'].b_amount);
            // $('#product_plan_edit').val(data.product_plan);
            
            data['product_plan'].forEach(function(item, index) {
                
                $('#product_plan_edit').append($('<option>', {
                    id: item.id,
                    value: item.productplanID,
                    text: item.productplanID
                }));
                
                if(data['data'].product_plan == item.productplanID){
                    $('#'+item.id).attr('selected',true);
                }
            });

        })
    });

This is the method from controller:

public function getEditPlanSchedule($id)
{
    $item['data'] = QuotationPlanSchedule::find($id);
    $item['product_plan'] = ProductPlan::orderby('id', 'asc')->get();
    return response()->json($item);
}
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You have to clear all options before adding again:

$("#product_plan_edit").empty();
about 4 years ago · Juan Pablo Isaza Denunciar

0

Either .empty the container or don't use append (better for the DOM update)

I am a little confused about your use of data['data'].product_plan to test the ID. In any case you can see the principle

$('#product_plan_edit').html(
  data['product_plan'].map(item => `<option value="${item.productplanID}">${item.productplanID}</option>`).join('')
);
$('#product_plan_edit').val(data['data'].product_plan); // select item.productplanID === data['data'].product_plan
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda