如何解决Laravel foreach 循环因 old() 数据出现故障
我有一个表单,允许用户通过单击按钮添加新的输入行。出于某种原因,表单提交(故意将所需的行留空以测试表单重新填充),索引 #1 被跳过,并且每次提交表单时都会发生这种情况。此外,提交时添加了额外的行,我似乎无法弄清楚原因。
Here is a numbered (top right) gallery depicting the result.
Foreach 循环:
@foreach(old() as $key => $value)
@if($key !== '_token' && $key !== 'Save' && $key !== 'due_by')
<div class="grid grid-cols-11 gap-6">
<div class="col-span-3">
<label class="block font-medium text-sm text-gray-700" for="">Title</label>
<input autocomplete="off" class="form-input rounded-md shadow-sm mt-1 block w-full" value="{{old('title.'.$loop->index)}}" type="text" name="title[]">
</div>
<div class="col-span-3">
<label class="block font-medium text-sm text-gray-700" for="">Description</label>
<input autocomplete="off" class="form-input rounded-md shadow-sm mt-1 block w-full" value="{{old('description.'.$loop->index)}}" type="text" name="description[]">
</div>
<div class="col-span-1">
<label class="block font-medium text-sm text-gray-700" for="">Quantity</label>
<input autocomplete="off" class="form-input rounded-md shadow-sm mt-1 block w-full" value="{{old('quantity.'.$loop->index)}}" type="text" name="quantity[]">
</div>
<div class="col-span-1">
<label class="block font-medium text-sm text-gray-700" for="">Price</label>
<input autocomplete="off" class="form-input rounded-md shadow-sm mt-1 block w-full" value="{{old('price.'.$loop->index)}}" type="text" name="price[]">
</div>
<div class="col-span-1">
<label class="block font-medium text-sm text-gray-700" for="">discount</label>
<input autocomplete="off" class="form-input rounded-md shadow-sm mt-1 block w-full" value="{{old('discount.'.$loop->index)}}" type="text" name="discount[]">
</div>
<div class="col-span-1 mx-auto">
<label class="block font-medium text-sm text-gray-700" for="taxable[{{$loop->index}}]">Taxable</label>
<div class="input-container">
<input autocomplete="off" type="hidden" name="taxable[{{$loop->index}}]" value="off" />
<input autocomplete="off" type="checkBox" class="toggle-input" id="taxable[{{$loop->index}}]" name="taxable[{{$loop->index}}]" value="on" @if(old('taxable.'.$loop->index) == 'on') checked @endif>
<label class="toggle switch" for="taxable[{{$loop->index}}]" style="margin-top: 10px;">
<div class="toggle-inner"></div>
</label>
</div>
</div>
<div class=" deleteField col-span-1 mx-auto"> <button style="margin-top:30px;" class="inline-flex items-center px-4 py-2 border border-transparent rounded-md font-semibold text-xs text-white uppercase tracking-widest bg-red-600 hover:bg-red-500 transition duration-300 ease-in-out transform hover:-translate-y-1 hover:scale-115"><i class="fas fa-trash-alt fa-lg"></i> </button></div>
</div>
@endif
@endforeach
jQuery:
$(document).ready(function() {
var max_fields = 100;
var wrapper = $(".container1");
var add_button = $(".add_form_field");
var x = 0;
$(add_button).click(function(e) {
e.preventDefault();
if (x < max_fields) {
x++;
$(wrapper).append('html here');
}
});
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。