1

這裏是我的按鈕: my $ post-> comment包含id,註釋,電子郵件和名稱; 我想將評論傳遞給模態內部的表單。 我也想用ID來路由的第二個參數來更新自己的評論Laravel 5.3將數據傳遞給Modal以編輯註釋

@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); 

});

回答

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'); 
    }); 

我希望我的回答能幫助您

相關問題