将数据从 Laravel 传递到引导模式

Hol*_*olo 5 php twitter-bootstrap laravel

想知道是否有人可以帮助我解决这个问题。我是 JS 和 JQuery 的完全新手。

我想避免在我的博客中删除帖子时发生意外,所以我想拥有它,这样当我点击删除时,它会弹出一个模式来确认我是否想要删除帖子,然后删除。

我添加了以下代码。

 <!-- Button trigger modal to delete post -->
<button type="button" class="btn btn-default btn-xs" data-toggle="modal" data-target="#deletePost">
  Delete
</button>

<!-- Modal -->
<div class="modal fade" id="deletePost" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">Delete Blog Post</h4>
      </div>
      <div class="modal-body">
        <p>You are about to delete a blog post, are you sure all that effort is to be sent to the unrecoverable trash can?</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default pull-left" data-dismiss="modal">OMG, NO!!!</button>
        <form action="/blog/{{ $post->id }}" method="POST">
            {{ csrf_field() }}
            {{ method_field('DELETE') }}
            <button type="submit" class="pull-right btn btn-danger">Delete it!</button>
        </form>
      </div>
    </div>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

现在一切正常,我现在要做的就是单击“删除它!” 按钮它会做到这一点。

除了删除正确的帖子之外,一切都有效,我想通过

{{ $post->id }}
Run Code Online (Sandbox Code Playgroud)

到模态,以便它将帖子ID发送到控制器。

我的问题是如何将上面的 id 传递给模态?我已经阅读了很多例子,它们都非常不同,所以我认为最好获得一些观点。

谢谢!

ITD*_*.eu 1

我非常推荐使用SweetAlert插件,它在视觉上与 Twitter Bootstrap 兼容,并且使用起来更加愉快。这是一段多语言与模型无关的删除功能的代码:

    $('body').on('click', 'sweet-delete', function (e) {
        e.preventDefault();
        var id = $(this).data('id');
        var model = $(this).data('model');
        swal({
            title: lang.confirm_please,
            text: lang.delete_warning,
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: lang.delete_button,
            closeOnConfirm: false,
            showLoaderOnConfirm: true,

        }, function(){
            $.post('/'+model+'/'+id, {_method: 'DELETE'}, function(data){
                swal({
                    title: lang.delete_title,
                    text: lang.delete_success,
                    type: "success",
                    timer: 5000
                }, function(){
                    window.location.href = '/'+model;
                });
            });
        });
    })
Run Code Online (Sandbox Code Playgroud)

而在你看来:

<button data-id="{{$post->id}}" data-model="blog" class="pull-right btn btn-danger sweet-delete">@lang('app.delete')</button>
Run Code Online (Sandbox Code Playgroud)

我必须提到,为了将 CSRF 令牌附加到我的 ajax 表单,我使用:

$.ajaxSetup({
        headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}
    });
Run Code Online (Sandbox Code Playgroud)

在我的 app.js 中,以及

<meta name="csrf-token" content="{{csrf_token()}}">
Run Code Online (Sandbox Code Playgroud)

在我的主布局文件中。