【问题标题】:Laravel Route to delete with Javascript/Bootstrap CheckboxLaravel Route 使用 Javascript/Bootstrap 复选框删除
【发布时间】:2016-05-31 19:03:00
【问题描述】:

我在 Laravel(PHP 框架)中编写了一个公式,这个公式获取了一些数据并将它们与我的控制器的路由一起发送。我的控制器具有从数据库中删除数据的功能。好吧,现在我想要一个 javascript/Bootstrap 复选框并找到了这个:

        <script>
            bootbox.confirm("Are you sure?", function(result) {
                Example.show("Confirm result: "+result);
            });
        </script>

这里有这个代码的寿命测试:http://bootboxjs.com

现在我想将此代码与我的 laravel 公式连接起来。有人可以帮我吗?我真的不怎么用javascript..

谢谢!

公式:

            {!! Former::horizontal_open()->method('DELETE')->action(action("Test\\TestController@destroy", $thread->id)) !!}
            {!! Former::large_danger_submit('Delete') !!} 
            {!! Former::close() !!}

当前代码:

{!! Former::horizontal_open()->method('DELETE')->action(action("Test\\TestController@destroy", $thread->id))->id('conf') !!}
            {!! Former::large_danger_submit('Delete') !!}
            <script>
                $('.conf').submit(function(event) {
                    event.preventDefault();
                    var subm = confirm("Are you sure?");
                    if(subm){
                        $('.conf p').text('Pressed Ok');
                        this.submit();
                    }else{
                        $('.conf p').text('Pressed Cancel');
                        return false;
                    }
                });
            </script>
            {!! Former::close() !!}

像这样获取 j.query、javascript 和引导程序:

整个master.blade.php:

<html>
<head>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous">
</head>
<body>
    @include("elements.navbar")
    <div class="container">
        <hr />

        @yield('content')
        <hr />
    </div>
@yield('custom-script')
    <script src="//code.jquery.com/jquery-1.12.0.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
</body>
</html>

带有表单的 HTML 和 JS 确认代码当然是获取 master.blade.php 的数据。因此支持查询和 JS/Bootstrap。

【问题讨论】:

  • 或者可能是另一种确认方式..

标签: javascript php twitter-bootstrap laravel


【解决方案1】:
**Jquery required.**

没有bootbox的另一种方式

<html>
    <head>
       //head
    </head>
    <body>
        @include("elements.navbar")
        <div class="container">
            <hr />
            @yield('content')
            <hr />
        </div>
        <script src="//code.jquery.com/jquery-1.12.0.min.js"></script>
        @yield('custom-script')
    </body>
</html> 

您的表单视图

{!! Former::horizontal_open()->method('DELETE')->action(action("Test\\TestController@destroy", $thread->id))->id('conf') !!}
    {!! Former::large_danger_submit('Delete') !!}
{!! Former::close() !!}
@section('custom-script')
<script>
//DOM Ready
$(function(){
    $('#conf').submit(function(event) {//form ID (not Class as you have ->id('conf'))
        event.preventDefault();
        var subm = confirm("Are you sure?");
        if(subm){
            console.log('Press Ok')
            this.submit();
        }else{
            console.log('Press Cancel')
            return false;
        }
    });
});
</script>
@stop

Working Fiddle

【讨论】:

  • 好吧,我需要不带 bootbox 的方式,因为我没有 bootbox 的软件包。我安装了 Jquery,但 bootbox 不在那里。你能告诉我如何连接我的公式的第二种方法?如果它是真的,去我的控制器中的destroy方法,否则什么都不做?不知道,JS迷惑了我
  • 好的,它现在在表单中,我没有收到任何错误,线程将被删除。并且 Jquery 在我的网络控制台中的 Javascript 之前加载 - 我只需要正常确认,如果它是真的,我将被重定向到另一个站点
  • 我的意思是,如果确认结果为真(按 OK),那么我将被重定向到另一个站点。否则什么都不会发生。这是我唯一想要得到的东西
  • 也试过了,但还是一样,只是删除线程,仅此而已,没有确认:/它在 Fiddle 中有效..这就是为什么我认为我的表单有问题。我会用我的表格和其他一些东西来更新我的问题
猜你喜欢
  • 1970-01-01
  • 2016-02-18
  • 2015-02-06
  • 1970-01-01
  • 2014-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-13
相关资源
最近更新 更多