2014-10-29 380 views
12

确认删除的问题,一切正常,但如果您在确认弹出窗口中单击取消,它将从数据库中删除该项目。有任何想法吗?请用代码示例显示,我是laravel的新手。确认使用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() !!} 

回答

17

只需添加回电给您的onsubmit电话。所以函数返回的值决定了表单是否被提交。

'onsubmit' => 'return ConfirmDelete()' 
+0

EXCELENT!谢谢! – David 2014-10-29 08:48:54

+0

不客气:) – lukasgeiter 2014-10-29 08:49:21

+0

非常感谢,链接很容易通过Laravel的'数据确认',但这已被困住了。 – malhal 2015-02-19 16:41:22

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> 
3

简单的放jQuery代码。

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

完美作品,谢谢 – 2017-10-03 21:03:45

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'=> '返回confirmDelete()'否则,我已经当了一些问题我没有确认问题就关闭了警报。

的JavaScript:

function confirmDelete() { 
var result = confirm('Are you sure you want to delete?'); 

if (result) { 
     return true; 
    } else { 
     return false; 
    } 
} 
+1

只需返回'结果' – peter 2017-01-24 09:03:40

1

没有任何函数调用...

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

例如使用laravel 5

0

只是想我要概括这出

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

位:

的.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(); 
    } 
});