【发布时间】:2021-08-19 11:46:09
【问题描述】:
我的编辑表单中有一个下拉菜单,但它没有在索引上显示所选值。
var ingredients=<?php echo $boms->load('rawMaterial') ?>;
console.log('BillOfMaterials', ingredients);
我将onClick=setIngredientsId 放在我的编辑按钮上。
function setIngredientsId(id){
ingredients.forEach(element => {
if(element.id==id){
console.log('hello', element.raw_material)
$('#modal-title-new-ingredients').html("Edit Ingredient")
$('#bom_id').val(element.id);
$('#ingredients').val(element.raw_material_id);
$('#consumption').val(element.quantity);
}
});
}
下面是我的下拉 HTML:-
<select class="noselecttwo form-control" name="ingredients" id="ingredients" required>
<option disabled selected value> -- Select a Ingredient -- </option>
@foreach ($ingredients as $ingredient)
<option id="var{{$ingredient->id}}" value="{{$ingredient->id}}">{{ $ingredient->name.' - '. $ingredient->rawMaterialUnit->name}}</option>
@endforeach
</select>
【问题讨论】:
-
我很确定你只会错过这个:` $('#ingredients').trigger('change');` 这样你就可以让 JS 更新 UI
-
$('ingredients option[value=' + element.raw_material_id + ']').prop('selected',true);? -
@RobBiermann 是的,终于!只漏了一行,非常感谢! :)
-
嘿@Shree,我猜你的也是正确的,但它不适用于我的代码。无论如何,Rob的回答解决了这个问题。也谢谢你! :)
标签: javascript html jquery laravel-8