I am developing a crud in php, with ajax and js
When I register a record in the corresponding modal, I have two selects in which I indicate the category to which it belongs, which stores the selected option in the table.
My problem comes when I want to edit some record if the select brings me the corresponding information, but in my select the information is duplicated. How should I do so that, when editing the registry, the select shows the corresponding options, without being repeated? I hope you can understand me, I share the code where I do the editing process, and screenshots. It should be noted that the recovery of the gang is done with an attr selected
//Editar
$(document).on("click", ".btnEditar", function () {
opcion = 2;//editar
fila = $(this).closest("tr");
id = parseInt(fila.find('td:eq(0)').text()); //capturo el ID
folio = fila.find('td:eq(1)').text();
nombre = fila.find('td:eq(2)').text();
apellido = fila.find('td:eq(3)').text();
direccion = fila.find('td:eq(4)').text();
descripcion_domicilio = fila.find('td:eq(5)').text();
telefono = fila.find('td:eq(6)').text();
num_contrato = fila.find('td:eq(7)').text();
fecha_inicio = fila.find('td:eq(8)').text();
fecha_final = fila.find('td:eq(9)').text();
cuadrilla = fila.find('td:eq(10)').text();
estatus = fila.find('td:eq(11)').text();
observaciones = fila.find('td:eq(12)').text();
encargado_cuadrilla = fila.find('td:eq(13)').text();
otra_descripcion = fila.find('td:eq(14)').text();
$("#folio").val(folio);
$("#nombre").val(nombre);
$("#apellido").val(apellido);
$("#direccion").val(direccion);
$("#descripcion_domicilio").val(descripcion_domicilio);
$("#telefono").val(telefono);
$("#num_contrato").val(num_contrato);
$("#fecha_inicio").val(fecha_inicio);
$("#fecha_final").val(fecha_final);
$("#cuadrilla option").text(cuadrilla).attr('selected', '')
$("#estatus option").text(estatus).attr('selected', '')
$("#observaciones").val(observaciones);
$("#encargado_cuadrilla").val(encargado_cuadrilla);
$("#otra_descripcion").val(otra_descripcion);
$(".modal-header").css("background-color", "#007bff");
$(".modal-header").css("color", "white");
$(".modal-title").text("Editar Reporte");
$('#modalCRUD').modal('show');
});
Here I show my selection that I show when registering a record in my modal
<div class="form-group">
<label for="" class="col-form-label">Cuadrilla</label>
<select name = "cuadrilla" id="cuadrilla" class="form-control" required>
<option selected>Seleccione una opción</option>
<option value="1">Operación ciudad</option>
<option value="2">Operación megasistema</option>
<option value="3">Drenajes</option>
<option value="4">Tomas tapadas</option>
<option value="5">Cloracion</option>
<option value="5">Bacheo</option>
</select>
</div>
Here I show screenshots of how the record obtained from the database repeats.
When pressing the edit button I have to appear, these options and not repeated.
fixed
$("#gang option:contains(" + gang + ")").attr('selected', '');