用laravel表单确认删除?

时间:2014-10-29 08:20:19

标签: php forms laravel confirm

确认删除问题,一切正常,但如果在确认弹出窗口中单击取消,则会从DB中删除该项目。有任何想法吗?请用代码示例显示,我是laravel的新手。

<script>

  function ConfirmDelete()
  {
  var x = confirm("Are you sure you want to delete?");
  if (x)
    return true;
  else
    return false;
  }

</script>

{!! Form::open(['method' => 'DELETE', 'route' => ['path_delete_time', $time->id], 'onsubmit' => 'ConfirmDelete()']) !!}

  {!! Form::hidden('case_id', $project->id, ['class' => 'form-control']) !!}

  {!! Form::button('<i class="glyphicon glyphicon-trash"></i>', array('type' => 'submit', 'class' => 'specialButton')) !!}

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

9 个答案:

答案 0 :(得分:24)

只需将回复添加到onsubmit来电即可。所以函数返回的值决定了表单是否被提交。

'onsubmit' => 'return ConfirmDelete()'

答案 1 :(得分:4)

在提交表单或删除记录时,简单地为提示框输入jQuery代码。

jQuery(document).ready(function($){
     $('.deleteGroup').on('submit',function(e){
        if(!confirm('Do you want to delete this item?')){
              e.preventDefault();
        }
      });
});

答案 2 :(得分:4)

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script>
        $(".specialButton").click(function(){
            return confirm("Do you want to delete this ?");
        });
    </script>

答案 3 :(得分:3)

你可以给表单一个id,并使用jQuery:

 {!! Form::open(['method' => 'DELETE', 'route' => ['path_delete_time', $time->id], 'id' => 'FormDeleteTime']) !!}

   <script type="text/javascript">
     $("#FormDeleteTime").submit(function (event) {
                 var x = confirm("Are you sure you want to delete?");
                    if (x) {
                        return true;
                    }
                    else {

                        event.preventDefault();
                        return false;
                    }

                });
</script>

答案 4 :(得分:2)

在我的新项目(Laravel 5.3)中,我正在使用以下内容:

{!! Form::open(['method' => 'DELETE', 'route' => ['admin.user.delete', $user], 'onsubmit' => 'return confirmDelete()']) !!}
    <button type="submit" name="button" class="btn btn-default btn-sm">
        <i class="fa fa-trash-o"></i>
    </button>
{!! Form::close() !!}

提及'onsubmit'=&gt; 'return confirmDelete()'否则当我在没有确认问题的情况下关闭警报时我遇到了一些问题。

JavaScript的:

function confirmDelete() {
    return confirm('Are you sure you want to delete?');
}

答案 5 :(得分:2)

没有任何函数调用...

'onsubmit' => 'return confirm("are you sure ?")'

示例

            {!! Form::open(['url'=>'posts/'.$post->id.'/delete','method'=>'DELETE','class'=>'form-horizontal',
        'role'=>'form','onsubmit' => 'return confirm("are you sure ?")'])!!}

使用laravel 5

答案 6 :(得分:0)

现在唯一发生的事情就是你得到一个弹出窗口,它会将重定向延迟到删除页面。

你可能会做的只是生成一个链接,然后在js中生成一个表单。 Like what they're doing here (only with JQuery)

答案 7 :(得分:0)

我想我会稍微概括一下:

html的

<form method="post" action="..." class="has-confirm" data-message="Delete this Thing?">...</form>

的.js

$("form.has-confirm").submit(function (e) {
  var $message = $(this).data('message');
  if(!confirm($message)){
    e.preventDefault();
  }
});

答案 8 :(得分:0)

我知道已经有一段时间了,但这是IMO最流畅的方法:

'onsubmit' => 'return confirm("Do you want to delete this user?");'

在表单标签中。