首页 > 解决方案 > 动态表单未发送所有值

问题描述

我有一个表单,我可以使用 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>

标签: javascriptphparrayslaravel

解决方案


推荐阅读