javascript - 动态表单未发送所有值
问题描述
我有一个表单,我可以使用 Javascript 进行更动态的添加,这很有效;但是当我将值发送到控制器时,我只从最后填写的表格中获取数据,不知道为什么。我正在使用 laravel 5.4。
我正在放置整个视图代码,以防万一我没有看到某些东西,也许其他人会看到。
{!! Form::open(array('route' => 'items.store', 'method' => 'POST'), array('role' => 'form')) !!}
<div class="input-group control-group after-add-more" id="formulario">
<div class="row">
<div class="form-group">
{!! Form::label('cantidad[]', 'Cantidad') !!}
{!! Form::text('cantidad[]', null, array('placeholder' => 'Ingresa cantidad del item', 'class' => 'form-control')) !!}
</div>
<div class="form-group">
{!! Form::label('detalle[]', 'Detalle') !!}
{!! Form::text('detalle[]', null, array('placeholder' => 'Ingresa detalle del artículo', 'class' => 'form-control')) !!}
</div>
<div class="form-group">
{!! Form::label('unidad[]', 'Unidad') !!}
{!! Form::text('unidad[]', null, array('placeholder' => 'Ingresa la unidad de medida (cm, m, kg; etc)', 'class' => 'form-control')) !!}
</div>
<div class="form-group">
{!! Form::label('unitario[]', 'Unitario') !!}
{!! Form::text('unitario[]', null, array('placeholder' => 'Indica el valor unitario del item', 'class' => 'form-control')) !!}
</div>
</div>
</div>
{!! Form::button('Guardar', array('type' => 'submit', 'class' => 'btn btn-primary')) !!}
{!! Form::close() !!}
<div class="form-group hide" id="cloneMe">
<div class="form-group">
{!! Form::label('cantidad', 'Cantidad') !!}
{!! Form::text('cantidad', null, array('placeholder' => 'Ingresa cantidad
del item', 'class' => 'form-control')) !!}
</div>
<div class="form-group">
{!! Form::label('detalle', 'Detalle') !!}
{!! Form::text('detalle', null, array('placeholder' => 'Ingresa detalle del artículo', 'class' => 'form-control')) !!}
</div>
<div class="form-group">
{!! Form::label('unidad', 'Unidad') !!}
{!! Form::text('unidad', null, array('placeholder' => 'Ingresa la unidad de medida (cm, m, kg; etc)', 'class' => 'form-control')) !!}
</div>
<div class="form-group">
{!! Form::label('unitario', 'Unitario') !!}
{!! Form::text('unitario', null, array('placeholder' => 'Indica el valor unitario del item', 'class' => 'form-control')) !!}
</div>
<button class="btn btn-primary" value="borrame">Borrar</button>
</div>
<script type="text/javascript">
$(document).ready(function() {
var max_fields = 10; //maximum input boxes allowed
var wrapper = $("#formulario"); //Fields wrapper
var add_button = $("#botoncito"); //Add button ID
var x = 1; //initlal text box count
$(add_button).click(function(e){ //on add input button click
e.preventDefault();
if(x < max_fields){ //max input box allowed
x++; //text box increment
var cloneHtml = $('#cloneMe').html();
$(wrapper).append(cloneHtml); //add input box
}
});
$(wrapper).on("click",".remove_field", function(e){ //user click on remove text
e.preventDefault(); $(this).parent('div').remove(); x--;
})
});
</script>
解决方案
推荐阅读
- c++ - 自动地?任务完成时加入 std::thread
- javascript - 如何让按钮调用函数?
- react-native - React Navigation - 嵌套重定向
- python - Scapy 将所有数据包设置为 BROWSER 并通过 IP 进行交互
- python - 使用 Python 打印 Min1 和 Min2
- c# - 将泛型数据类型接受到类构造函数列表中
- c# - Xamarin Forms Pinch 缩放事件未引发
- java - 如果我不对双精度进行任何操作,我可以在 Java 中保持精度吗?
- c++ - 在 C++ 中将集合转换为向量和将向量转换为字符串的运行时异常
- angular - Angular PrimeNG 日历 - 有没有办法在使用 selectionMode="range" 时设置最短结束日期?