Im trying to open a edit modal, and load the results of this query.
My problems is the function “onchange” continue loading after the element was selected and changed it.
This is the function on the button to open the modal
const editModal = async (id, company, element) => {
$("#companyEdit").val(insumo).select2().val();
$("#elementEdit").val(element).select2().val();
$("#edit").modal("show").attr("data-id", id);
};
This is the front
.modal-body
.row(style="padding:15px;")
.col-md-12.col-12
.form-group
label Company:
select#companyEdit(onChange="selectElement()")
option(disabled value=null) --Select--
option(value=0) Sin asignar
each item in company
option(value=item.id)= item.name
.col-md-12.col-12
.form-group
label Element:
.controls
select#elementEdit(disabled)
This code works but delete the selected company.
const selectElement= async () => {
let valueCompany= $("#companyEdit")
let valueElement= = $('#elementEdit')
const company_id= valueCompany.select2().val();
$.ajax({
method: "GET",
url: `/admin-elements/${company_id}`,
dataType: "json",
success: function (data) {
if (data.sucess == 200) {
valueElement.empty();
for (i = 0; i < data.result.length; i++) {
valueElement.append('<option value="' + data.result[i].name+ '">' + data.result[i].name+ '</option>');
}
valueElement.attr("disabled", false);
} else {
valueElement.append(
'<option value="">--</option>'
);
valueElement.attr("disabled", false);
}
},
error: function (e) {
...
},
})
}
Thanks very much!!
I want can open a modal and see the select with the correct value with the possiblity of changed it with a query.