第一个疑问是当我需要在浏览器中返回时如何记住选定的下拉菜单?我有几个字段,我输入所需的数据并选择所有下拉列表。如果有一些错误,重定向到具有特定错误的同一页面并且所有字段都已完成,但是下拉列表没有,我必须再次选择。这很烦人.. 我正在使用Laravel with Blade。我的选择的一个例子是下一个:
<div class="form-group">
<label class="col-md-3 control-label" for="country_id"> {{{ trans('provider.countries') }}} <span class="required">*</span></label>
<div class="col-md-6">
<div class="input-group btn-group">
<span class="input-group-addon">
<i class="fa fa-th-list"></i>
</span>
{!! Form::select('country_id', $countries, old('country_id'), array('class' => 'form-control', 'required', 'id' => 'select_country', 'placeholder'=>'Seleccione un País', 'autocomplete'=>'off')) !!}
</div>
</div>
</div>
另一方面,我得到了一个包含4列的数据表,1个带有select,2个带有inpput数字。当我添加第一行时,字段验证工作正常,但是当我添加多行时,此验证不起作用.. 代码是下一个
<div class="table-responsive table-striped table-bordered table-condensed">
<table class="table table-receipts invoice-items" id="receipt-table">
<thead>
<tr class="h5 text-dark">
<th id="cell-item" class="text-weight-semibold vertical-center">Producto</th>
<th id="cell-store" class="text-center text-weight-semibold absolute-center">Sucursal</th>
<th id="cell-price" class="text-center text-weight-semibold absolute-center">Precio Unitario</th>
<th id="cell-qty" class="text-center text-weight-semibold absolute-center">Cantidad</th>
<th id="cell-delete" class="text-center text-weight-semibold absolute-center">Acciones</th>
</tr>
</thead>
<tbody>
<tr class="hide default-row-product">
<td class="col-md-4 text-weight-semibold text-dark vertical-center">
<input type="hidden" name="products[]">
</td>
<td class="col-md-2 text-center">
{!! Form::select('stores[]', $stores, '', array('class' => 'form-control', 'required', 'placeholder'=>'Seleccionar', 'id'=>'stores_id')) !!}
</td>
<td class="col-md-1 text-center">
<input type="number" name="prices[]" class="form-control" min="1" step="0.1" id="prices_id" required>
</td>
<td class="col-md-1 text-center">
<input type="number" name="quantities[]" class="form-control" min="1" step="0.1" id="quantities_id" required>
<td class="col-md-1 text-center">
<button type="button" class="mb-xs mt-xs mr-xs btn btn-xs btn-danger" id="button-delete-product"><i class="fa fa-trash"></i></button>
</td>
</tr>
</tbody>
</table>
</div>
问候!
答案 0 :(得分:0)
回答第一个问题: 您必须将所选选项存储在本地存储或cookie中:
// store value in local storage
$('select[name="country_id"]').change(function() {
localStorage.setItem('selected', this.value);
});
// restore selected value from local storage after page was loaded
if(localStorage.getItem('selected')){
$('select[name="country_id"]').val(localStorage.getItem('selected'));
}
或者您可以使用会话。在验证数据后的控制器中,在会话中放置了选定选项。
if($validator->fails) {
return Redirect:to('/form')->with('selected', Input::get('question_id');
}
在View中使用jQuery:
@if(Session::has('selected')
$(document).ready(function() {
$('select[name="country_id"]').val("{{ Session::get('selected') }}");
});
@endif
编辑: 验证输入数组(价格):
$prices = Input::get('prices');
// create array for each price input
for($i = 0; $i <= count($prices); $i++){
$rules['prices.' . $i] = 'required|numeric|between:0,100';
}
$validator = Validator::make($prices, $rules);