Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript sweetalert删除确认laravel_Javascript_Laravel_Parameter Passing_Laravel Blade_Sweetalert - Fatal编程技术网

Javascript sweetalert删除确认laravel

Javascript sweetalert删除确认laravel,javascript,laravel,parameter-passing,laravel-blade,sweetalert,Javascript,Laravel,Parameter Passing,Laravel Blade,Sweetalert,我在laravel中遇到一个问题,无法通过sweetalert确认删除帖子 <script> !function ($) { "use strict"; var SweetAlert = function () { }; //examples SweetAlert.prototype.init = function () { $('.sa-remove').cl

我在laravel中遇到一个问题,无法通过sweetalert确认删除帖子

<script>
        !function ($) {
        "use strict";
        var SweetAlert = function () {
        };
        //examples 
        SweetAlert.prototype.init = function () {

            $('.sa-remove').click(function () {
                swal({
                    title: "are u sure?",
                    text: "lorem lorem lorem",
                    type: "error",
                    showCancelButton: true,
                    confirmButtonClass: 'btn-danger waves-effect waves-light',
                    confirmButtonText: "Delete",
                    cancelButtonText: "Cancel",
                    closeOnConfirm: true,
                    closeOnCancel: true
                },
                function(){
                    window.location.href = "{{ route('panel.posts.remove',$post->id) }}";
                });
            });
        },
        //init
        $.SweetAlert = new SweetAlert, $.SweetAlert.Constructor = SweetAlert
}(window.jQuery),

//initializing 
    function ($) {
        "use strict";
        $.SweetAlert.init()
    }(window.jQuery);
</script>    

当然我是新来的

问题在于您的url中,没有自动选择需要删除的模型id,这是因为您在url上的javascript中只打印了$post->id,这是您不应该做的事情,因为这样混合php和js是不好的做法

因此,为了解决您的问题,您应该正确设置href,或者直接将其插入href属性,或者不将php与js混合使用,例如:使用js选择器而不是php选择$post->id,您试图将php打印到javascript上,这没有任何意义。js中的php代码将运行一次,这就是为什么它会打印最后一个id,而不是您单击的元素

你应该试着做一些像…:

function(){
                window.location.href =  // select post id with js here;
            });
但是我要做的是在你的foreach上设置href,这样你就已经设置好并准备好发布到你需要的路线,这样做会更有意义

<a href="/your-delete-route/{{$post->id}}" class="sa-remove"><button class="wave-effect btn btn-danger btn-bordred wave-light"><i class="fa fa-times"></i></button></a>

如果使用多条记录,则可以使用此完全动态的代码:

<a href="{{ route('users.destroy', $entity->id) }}"
    class="confirmation"
    data-title="Delete User"
    data-text="Are you sure want to delete this user? ">
     <i class="icon-trash"></i>
</a>
生成函数,然后确认并将用户重定向到删除方法:

function warnBeforeRedirect(linkURL, _text, _title) {
    swal({
        title: _title,
        text: _text,
        type: 'warning',
        showCancelButton: true,
        html: true,
    }, function () {
        var form = $('<form>', {
            'method': 'POST',
            'action': linkURL
        });

        var hiddenInput = $('<input>', {
            'name': '_method',
            'type': 'hidden',
            'value': 'DELETE'
        });

        hiddenToken = $('<input>', {
            'name': '_token',
            'type': 'hidden',
            'value': jQuery('meta[name="csrf-token"]').attr('content')
        });

        form.append(hiddenInput).append(hiddenToken).appendTo('body').submit();
    });
}
函数warnBeforeRedirect(链接URL、\u文本、\u标题){
游泳({
标题:_标题,
文本:_text,
键入:“警告”,
showCancelButton:true,
是的,
},函数(){
变量形式=$(''{
'method':'POST',
“操作”:链接URL
});
变量hiddenInput=$(''{
'名称':'方法',
“类型”:“隐藏”,
“值”:“删除”
});
hiddenToken=$(''){
'名称':'标记',
“类型”:“隐藏”,
'value':jQuery('meta[name=“csrf token”]').attr('content')
});
append(hiddenInput).append(hiddenToken.appendTo('body').submit();
});
}
如果您使用的是Laravel DELETE Route,那么您还需要将令牌传递到hidden。所以我创建了表单,并用带有一些隐藏变量的Body标记将其追加。那就提交吧


希望对你有帮助。祝你好运。

你删除了错误的模态对象。首先,你应该向链接按钮添加一个数据属性

 <a href="#" data-id="{{$post->id}}" class="sa-remove"><button class="wave-effect btn btn-danger btn-bordred wave-light"><i class="fa fa-times"></i></button></a> code here

谢谢!但我不想使用POST方法。。。有没有办法不改变这一切?我的代码完全错了吗?谢谢,伙计!这正是我想要的,而且效果很好。
function warnBeforeRedirect(linkURL, _text, _title) {
    swal({
        title: _title,
        text: _text,
        type: 'warning',
        showCancelButton: true,
        html: true,
    }, function () {
        var form = $('<form>', {
            'method': 'POST',
            'action': linkURL
        });

        var hiddenInput = $('<input>', {
            'name': '_method',
            'type': 'hidden',
            'value': 'DELETE'
        });

        hiddenToken = $('<input>', {
            'name': '_token',
            'type': 'hidden',
            'value': jQuery('meta[name="csrf-token"]').attr('content')
        });

        form.append(hiddenInput).append(hiddenToken).appendTo('body').submit();
    });
}
 <a href="#" data-id="{{$post->id}}" class="sa-remove"><button class="wave-effect btn btn-danger btn-bordred wave-light"><i class="fa fa-times"></i></button></a> code here
 $('.sa-remove').click(function () {
            var postId = $(this).data('id'); 
            swal({
                title: "are u sure?",
                text: "lorem lorem lorem",
                type: "error",
                showCancelButton: true,
                confirmButtonClass: 'btn-danger waves-effect waves-light',
                confirmButtonText: "Delete",
                cancelButtonText: "Cancel",
                closeOnConfirm: true,
                closeOnCancel: true
            },
            function(){
                window.location.href = "your-url/" + postId;
            }); here