Laravel 5.3将数据传递给Modal以编辑Comment

时间:2017-07-04 09:30:57

标签: laravel-5 modal-dialog laravel-5.3 bootstrap-modal modalviewcontroller

这是我的按钮: 我的$ post->评论包含id,评论,电子邮件和姓名; 我想将注释传递给模态中的表单。 我还想使用id到路由的第二个参数来更新注释

@foreach($post->comments as $comment)
    <button class="btn btn-success btn-xs " data-toggle="modal" data-target="#myModal" data-id="{{ $comment->id }}" data-comment=" {{ $comment->comment }}"><i class="fa fa-pencil"></i></button>
    @endforeach

这是我的模态:

  <!-- Modal -->
  <div class="modal fade modal-md" id="myModal" role="dialog">
    <div class="modal-dialog">
      <!-- Modal content-->
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;
          </button>
          <h4 class="modal-title">Comment Update!</h4>
        </div>
        <div class="modal-body">
        <div id="comment-form" class="">
        {{ Form::open(['route' => ['comments.store', $post->id], 'method' => 'POST']) }}
        <div class="row">
        <div class="col-md-12">
        <b>{{ Form::label('comment', "Comment:") }}</b>
        {{ Form::textarea('comment', null, ['class' => 'form-control', 'rows' => '5', 'id'=>'comment ']) }}
        </div>
        </div>
        {{ Form::close() }}
        </div>
        </div>
        <div class="modal-footer">
        <a href="{{route('comments.store',$post->id) }}"
        onclick="event.preventDefault();
        document.getElementById('comment-update').submit();"  class="btn btn-primary">
            Update </a>
        <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
        </div>
        </div> 
        </div>
        </div>

我试过这个脚本,但它无法工作:

<script type="text/javascript" charset="utf-8">
$('#myModal').on('show', function(e) {
var link = e.relatedTarget();

var id = link.data("id");
var comment = link.data("comment");

var modal = $(this);
modal.find("#id").val(id);
modal.find("#comment").val(comment);

});

1 个答案:

答案 0 :(得分:0)

你必须使用javascript中的点击事件

示例:

<button class="btn btn-success btn-xs add" data-toggle="modal" data-target="#myModal" data-id="{{ $comment->id }}" data-comment=" {{ $comment->comment }}"><i class="fa fa-pencil"></i></button>

这个脚本。

$(document).on('click', '.add', function() {
      $('#id_kategori').val($(this).data('id'));
      $('#comment').val($(this).data('comment'));
      $('.form-horizontal').show();
      $('#myModal').modal('show');
  });

我希望我的回答可以帮助你