doulian8742 2018-03-01 13:30
浏览 29

通过动态字段输入::旧Laravel

I have a dynamic field and I can't get the old value because I added this dynamic field with jQuery, but after validation I don't have the fields.

My jQuery code:

var fielddd = 1;
$(document).on('click', '.btn.add-field', function() {
   fielddd++;
   $('#newfield').append('<textarea class="from-control" name="somename[]" id="field_' + field + '"></textarea>' + '<button class="btn add-field">add</button>' + '<textarea class="from-control" name="somename2[]" id="field_' + field + '"></textarea>');
});

HTML code:

<div class="row">
   <div class="{{($errors->has('somename[]')}}"
       <textarea class="from-control"  name="somename[]" id="field"> 
       </textarea>
{!! $errors->first('somename[]',:message) !!}
</div>
    <button class="btn add-field">add</button>
    <div class="{{($errors->has('name[]')}}"
          <textarea class="from-control" name="name[]" id="field">
         </textarea>
    {!! $errors->first('somename[]',:message) !!}
   </div>
</div>

<div class="row" id="newfield">
    <!-- new textarea -->
</div>

How can I get the same fields with the same input after validation?
I for example some field is empty. If I press add field, and after that pressing the button submit the laravel check if the field is empty and if yes then it redirect to the same page and say to put some value inside the text area.

  • 写回答

1条回答 默认 最新

  • dpd46554 2018-03-01 14:15
    关注

    You could print out the old response in JavaScript in the form, that you could then fetch from. If you printed out old('somefield[]') as JSON, you could then read each value and re-populate the form with textareas.

    var oldValues = json_encode(old('somefield[]'))
    

    Then you could use oldValues

    oldValues.forEach(function(oldValue) {
        // create new textarea using oldValue as value
    })
    
    评论

报告相同问题?

悬赏问题

  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 Macbookpro 连接热点正常上网,连接不了Wi-Fi。
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题