【发布时间】:2021-08-20 05:16:04
【问题描述】:
我有一个共享相同data-target 的模式。如果我单击edit 按钮,然后关闭并单击add 按钮,模态将显示编辑模态而不是添加模态。如何更改表格?
添加按钮:-
<button type="button" class="btn btn-sm btn-primary" data-toggle="modal" data-target="#modal-new-ingredients" onClick=(setRestaurantId({{ $restorant_id }}))>{{ __('Add') }}</button>
编辑按钮:-
<button type="button" class="dropdown-item" data-toggle="modal" data-target="#modal-new-ingredients" onClick=(setIngredientsId({{ $bom->id }}))>Edit</button>
setRestaurantId 函数:-
function setRestaurantId(id){
$('#res_id').val(id);
// $('#form')[0].reset();
// $('#ingredients').trigger('change');
$('#modal-title-new-ingredients').html("Add Ingredient")
$('#ingredients').val("");
$('#consumption').val("");
}
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);
$('#ingredients').trigger('change');
}
});
}
HTML:-
<form role="form" method="post" action="{{ route('bom.store', $item->id) }}" enctype="multipart/form-data" id="form">
@csrf
<div class="form-group">
<label class="form-control-label" id="form-ingredients" for="ingredients">{{ __('Ingredients') }}</label>
<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>
</div>
<div class="form-group">
<label class="form-control-label" for="consumption">{{ __('Consumption') }}</label>
<input type="number" step="any" name="consumption" id="consumption" class="form-control" placeholder="{{ __('Consumption') }}" value="" required>
</div>
@include('partials.input',['type'=>"hidden",'name'=>'bom_id','id'=>'bom_id','required'=>false,'placeholder'=>'id'])
<div class="text-center">
<button type="submit" class="btn btn-primary my-4">{{ __('Save') }}</button>
</div>
</form>
如果需要,成分的模态 HTML:-
<div class="modal fade" id="modal-new-ingredients" tabindex="-1" role="dialog" aria-labelledby="modal-form" aria-hidden="true">
【问题讨论】:
-
嗨,也显示 html。另外,
if I change the value inside manually, it will update the value..是什么意思? -
嗨@Swati,已经添加了 HTML 并更正了我的句子。谢谢你
标签: javascript html jquery forms modal-dialog