Laravel模态隐藏值调用路由

时间:2019-03-09 03:14:39

标签: javascript php laravel bootstrap-4

我有一个模态,其中包含我需要的当前ID的隐藏字段。

我需要在我的模式中单击一个按钮,以确认是否应该删除用户。我设置了一个不确定的表单,不确定这是否是最好的选择,并且在javascript中将表单的attr设置为该路由,但是该路由未找到正确的路径,尽管在URL中它说的是正确的我可能缺少的路由用PHP代替JS?

<div class="modal" id="mdelete" role="dialog" aria-labelledby="moddelete">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="moddelete">Confirm Delete</h5>
          <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
          </button>
      </div>
      <div class="modal-body">
        <p>Are you sure you want to delete</p>
      </div>
      <div class="modal-footer">
        <form method="POST" id="formdelete">
          <input type="hidden" name="txtid" id="txtid" />
          <input type="text" name="uid" id="uid" />
          <button type="button" class="btn btn-danger " data-dismiss="modal">No</button>
          <span class="text-right">
            <button type="submit" class="btn btn-primary btndelete">Yes</button>
          </span>
        </form>
      </div>
    </div>
    <a href="{{ route('users.show', $user->id) }}" class="btn btn-primary mr-2">Show</a>
    <a href="{{ route('users.edit', $user->id) }}" class="btn btn-info text-white mx-2">Edit</a>
    <button type="button" class="btn btn-danger ml-2" data-toggle="modal" 
            data-target="#mdelete" data-id="{{$user->id}}" 
            data-name="{{$user->username}}">Delete</button>

    $(document).ready(function() {
      $('#mdelete').on('show.bs.modal', function (event) {
        var button = $(event.relatedTarget);
        var userid = button.data('id');
        var uname = button.data('name');
        var modal = $(this);
        modal.find('#txtid').val(userid);
        modal.find('#uid').val(userid);
        modal.find('.modal-body').text('Are you sure you want to delete ' + uname);
      })
      $('#formdelete').submit(function() {
        var userid = $('#txtid').val();
        $('#formdelete').attr("action", "route('$users.destroy',$user->"+ userid +")");
        $('#formdelete').submit();
      });
    });

1 个答案:

答案 0 :(得分:0)

您尝试生成路由的尝试将失败,因为您没有正确使用模板:

$('#formdelete').attr("action", "route('$users.destroy',$user->"+ userid +")");

您可以尝试让您的路线接受可选的用户字段,然后生成到该字段的路线,并使用js附加用户ID值,如下所示:

<script>
    $(function() {
        var form = $('#formdelete');
        var path = '{{ route("users.destroy") }}';

        $('#formdelete').submit(function(event) {
            var form = $(this);
            var userid = form.find('#txtid').val();
            $('#formdelete').attr("action", path + '/' + userid);
            $('#formdelete').submit();
        }); 
    });
</script>