Laravel Form with Validation和store Jquery添加了html元素



我使用Laravel,我有一个带有Laravel验证和必填字段的表单(所以当我遇到错误时,它总是重新加载所有页面,我用旧的blade标签保存值(。一切都很好,但我用jquery 做了一个按钮添加了一些文本框

$(".add-more").click(function(){ 
var html = $(".copy").html();
$(".after-add-more").before(html);
});
$("body").on("click",".remove",function(){ 
$(this).parents(".control-group").remove();
});

当Laravel在提交后的每个无效消息处重新加载所有页面时,我如何保存旧值和创建的n-html元素?Thx

它很简单,你不重新加载整个页面,用laravel的@include包含一个元素。例如,表的内容在另一个刀片文件中,但它包含在中

<table class="table tablesorter ">
<thead class=" text-primary">
<tr>
<th scope="col">ID</th>
<th scope="col">TIPO</th>
<th scope="col">fecha</th>
<th scope="col">Monto</th>
<th scope="col">Acciones</th>
</tr>
</thead>
<tbody id="table">
@include('component-table')
</tbody>
</table>

组件表.blade.php


<tr>
<td>hi</td>
<td>
test
</td>
<td></td>
<td></td>
<td class="text-right">
</td>
</tr>

现在,您可以创建一个返回此视图的控制器,并在调用该控制器时仅重新加载表,然后用jquery的html((函数替换html

//on controller 
public function table()
{
return view('component-table.blade.php');
}
//js
async function reloadTable(){
await $.ajax({
type: "GET",
url: "{{ route('component-table') }}",
data: '',
success: function(response){
$('#table').html(response)
}
});
}

至少在使用刀片时,我得到了这个,我不知道是否正确。恢复我所拥有的。一个表单,有几个字段和一个按钮,每次按下这个按钮都会添加另外2个html字段(其中一个是组合框,名称为'tipo',从数据库中的表(称为moyens(中获取值(

@if( old('tipo') )
@foreach (old('tipo') as $key => $value) 
<!--<div>{{ $key . ' => ' . $value }}</div>-->
<div class="col-sm-5">
<select class="form-control" name="tipo[]">
@foreach($moyens as $moyen)
<option value="{{$moyen->code}}" @if($moyen->code == '$value') 'selected' @endif>{{ $moyen->name }}</option>
@endforeach
</select>
</div>
<div class="col-sm-4">
<div class="input-group-prepend">
<span class="input-group-text">&#8364</span>
<input type="number" min="0" step="0.01" data-number-to-fixed="2" data-number-stepfactor="100" class="form-control" name="importo[]" placeholder="importo" value="{{old('polizza')}}">
</div>
</div>
<div class="col-sm-3">
<button class="btn btn-danger remove" type="button"><i class="glyphicon glyphicon-remove"></i> Cancella</button>
</div>
@endforeach
@endif

所有工作,但我无法在组合框/es 中检索旧的选定值

这部分是吗

<option value="{{$moyen->code}}" @if($moyen->code == '$value') 'selected' @endif>{{ $moyen->name }}</option>

能纠正这种做法吗?Thx

最新更新