单击表单提交前确认对话框

时间:2017-05-04 08:01:49

标签: jquery laravel onclick

我正在使用Laravel 5.4。我有POST路线:

Route::post('hotel/delete/{slug}', ['as' => 'delete-hotel', 'uses' => 'Backend\HotelController@postDelete']);

在索引页面中,我有一个带表单提交的锚标记。目前,当点击锚标记时,会提交隐藏的表单。

<a href="{{ route('delete-hotel', $hotel->slug) }}" class="btn btn-operation btn-danger" 
    onclick="event.preventDefault(); 
    document.getElementById('delete-form').submit();"> 
    <i class="fa fa-close"></i> Delete</a> 

<form id="delete-form" action="{{ route('delete-hotel', $hotel->slug) }}" 
    method="post" style="display: none;">
    {{ csrf_field() }}
</form>

但是,我想显示一个确认对话框。从用户点击结果中,如果用户点击ok,则删除该项目,或者如果用户点击cancel,则不会采取任何措施。

非常感谢。谢谢

3 个答案:

答案 0 :(得分:2)

我认为你应该使用javascript默认confirm()

HTML PART

<a href="{{ route('delete-hotel', $hotel->slug) }}" class="btn btn-operation btn-danger" onclick="return confirmation();"> 

<强> SCRIPT

function confirmation(){
    if(confirm('are you sure?')){
        document.getElementById('delete-form').submit();
    }else{
        return false;
    }   
}

希望它会对你有所帮助。

<强>更新

可能是您在表单提交前重定向到href网址。所以建立你的链接

<a href="javascript:void(0)" class="btn btn-operation btn-danger" onclick="return confirmation();">

答案 1 :(得分:0)

http://bootstrap-confirmation.js.org/

这是一个非常有用的工具!

将以下代码放入Document ready中。

    $('[id$="btnDoSomethingDrastic"]').click(function (event) {
    //So when a user clicks the DoSomethingDrastic button....

    event.preventDefault();
    // If one already exists then destroy it
    $(this).confirmation('destroy');

    // Create a new confirmation...
    $(this).confirmation({
        rootSelector: '[data-toggle=confirmation]',
        placement: 'bottom',
        title: 'Action Selected Changes',
        btnOkLabel: 'Action now',
        btnOkIcon: 'glyphicon glyphicon-share-alt',
        btnOkClass: 'btn-success',
        btnCancelLabel: 'Cancel',
        btnCancelIcon: 'glyphicon glyphicon-ban-circle',
        btnCancelClass: 'btn-danger',
        content: 'Are you sure you want to continue:<br><br>This will do something drastic!!!<br><strong>Are you sure you want to do this?</strong>',
        html: true,
        popout: true,

        container: 'body',
        onConfirm: function () {
            //The user confirmed they wanted to do something drastic
            actionGoAndDoSomethingDrastic();
        }
    });

    // ... and show it.
    $(this).confirmation('show');
});

答案 2 :(得分:0)

使用Jquery(可以不用,但因为它被广泛使用......):

$(document).on("click", '.delete_button',function() {
    var data = $('#delete-form').serialize();
    slug = $(this).attr('href');
    var confirm = confirm("Are you sure ?");
    if (confirm) {
        $.ajax({
            url: slug,
            type: "get",
            data : 'data',
            success : function(data){
                [Do what you want (checkmark, alert, etc ....)]
            }
        });
    event.preventDefault();
    }
});