【问题标题】:confirm dialog before form submit on click点击表单提交前确认对话框
【发布时间】:2017-10-02 07:05:34
【问题描述】:

我正在使用 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,则不执行任何操作。

感谢您的帮助。谢谢你

【问题讨论】:

    标签: jquery laravel onclick


    【解决方案1】:

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

    HTML 部分

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

    脚本

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

    希望对你有所帮助。

    更新

    可能是您在提交表单之前重定向到href url。所以做你的链接

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

    【讨论】:

    • 感谢您的回答。这将显示一个确认对话框,并根据该事件提交表单。但是,表单是使用GET METHOD 提交的。有什么想法吗?
    • 添加属性method="POST"到表单
    • 方法已定义为发布,但实际上并未触发。 @sanchit
    • 尝试删除href标签的url,使其成为href="javascript:void(0)"进行测试。让我更新我的答案
    • 欢迎您@vijayrana!快乐编码:)
    【解决方案2】:

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

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

    将下面的代码放入准备好的文档中。

        $('[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');
    });
    

    【讨论】:

    • 如果你愿意,你可以自己写一些东西。
    • 这很棒,但需要一些时间和精力。将列在 TO DO 列表中。
    【解决方案3】:

    使用 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();
        }
    });
    

    【讨论】:

    • 你能解释一下If you only want to hit a GET route, you don't have to use a form
    • 没关系,我错过了阅读您的代码,您似乎发送了带有链接的表单。我编辑我的答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 2011-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多