I have dropdown like this:
<div class="row">
<div class="col-md-12 col-sm-6">
<div class="form-group">
<label for="">{{ __('Product Plan') }}<span class="required"></span></label>
<select name="product_plan" class="form-control" id="product_plan" style="width: 50%">
@foreach ($product_plan as $val)
<option value="{{ $val->productplanID }}">
{{ $val->productplanID }}</option>
@endforeach
</select>
</div>
</div>
</div>
I using modal to update this data, so I use AJAX. This is my ajax method for get the data:
$('body').on('click', '.editPlanSchedule', function() {
var Item_id = $(this).data('id');
$.get("/quotation/getEditPlanSchedule" + '/' + Item_id, function(data) {
$('#product_plan').val(data.product_plan);
// console.log(data.product_plan);
})
});
I get the data correctly. My question is, how to put the selected value in select option from database using ajax? I try console.log(data.product_plan), I get the data. But when I do $('#product_plan').val(data.product_plan);, select option not selected the correct select from database.
Try this:
$('body').on('click', '.editPlanSchedule', function() {
var Item_id = $(this).data('id');
$.get("/quotation/getEditPlanSchedule" + '/' + Item_id, function(data) {
let options = "";
data.product_plan.forEach(p => {
options += `<option value="${p.productplanID}">${p.productplanID}</option>`;
}
$('#product_plan').val(options);
})
});