Laravel模态隐藏值要调用路线



我有一个我需要的当前ID的隐藏字段的模态。

我需要单击模式中的按钮,以确认是否应该删除用户。我设置了一个表单不确定这是否是最佳选择,在JavaScript中,将形式的属性设置为路线的属性,但路线没有找到正确的路径在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();
      });
    });

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

$('#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>

相关内容

  • 没有找到相关文章

最新更新