在laravel中删除带有模态的项目

时间:2017-01-13 13:40:41

标签: php jquery laravel routes bootstrap-modal

我的项目旁边有一个删除按钮,只要单击该按钮,就会显示一个弹出模式以供确认。我想在模态中使用yes按钮,然后在单击此按钮时删除项目。

我用@include将模态调用到projects / show视图中。使用以下jquery单击按钮时会出现模态,但按下时按下yes按钮不会删除项目。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>


<script>
$(document).ready(function() {

   $(".delete").on("submit", function(e){

       e.preventDefault();

       $('#myModal').modal('show'); 


    });

});

项目旁边的删除按钮:

  {!! Form::open(['route' => ['projects.destroy', $projects->id], 'class' => 'delete', 'method' => 'DELETE']) !!}

           {!!Form::button('<span class="glyphicon glyphicon-remove"></span>', ['class' => 'btn btn-danger btn-md', 'type' => 'submit'])!!}

            {!! Form::close() !!}

模态代码:

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog modal-md" 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 text-center" id="myModalLabel">Delete Confirmation</h4>
      </div>
      <div class="modal-body text-center">
        Are you sure you want to delete this project?
      </div>
      <div class="modal-footer">
        <button type="submit" id="delete-btn" class="btn btn-default" >Yes</button>

        <button type="button" class="btn btn-primary" data-dismiss="modal">Cancel</button>
      </div>
    </div>
  </div>
</div>

ProjectController:

<?php

namespace App\Http\Controllers;

use App\project;
use App\Http\Requests;
use Illuminate\Http\Request;
use Session;

class ProjectsController extends Controller
{

 public function destroy($id){

        $project = Project::find($id);

        $project->delete();

        Session::flash('success', 'The project was successfully deleted!');

        return redirect()->route('projects.show', $project->project_id);

    }

}

1 个答案:

答案 0 :(得分:1)

将模式中的提交按钮更改为:

{!! Form::open(['route' => ['projects.destroy', $projects->id], 'class' => 'delete', 'method' => 'DELETE']) !!}

<button type="submit" id="delete-btn" class="btn btn-default" >Yes</button>

{!! Form::close() !!}

最佳解决方案是删除您现在拥有的删除按钮的实现,并将其设为普通按钮。让该按钮弹出模态并替换你的&#34;是&#34;按钮上面有一个像我上面那样的实际删除按钮。