简体   繁体   English

Laravel莫代尔在刀片中的脚本中不起作用

[英]Laravel modal dont functioning in script in blade

So i have a strange problem. 所以我有一个奇怪的问题。 I have a form modal that opens when i click a button, All i want to do is reopen the modal when there are validation errors so I do this. 我有一个表单模式,当我单击一个按钮时打开,我想要做的是当有验证错误时重新打开模态,所以我这样做。 In my blade template home.blade.php. 在我的刀片模板home.blade.php中。

<script type="text/javascript">
        @if (count($errors) > 0) {
            $('#myModal').modal('show');
        }


    </script>
@endsection

When I put an alert in the script above it works fine but the modal doesnt reopen. 当我在上面的脚本中发出警报时它工作正常,但模态不会重新打开。 ok and When i try opening my modal in my javascript file it works.Im confused would appreciate some help. 好的,当我尝试在我的javascript文件中打开我的模态时,它可以工作。我很困惑,会感激一些帮助。

Your blade syntax should be as: 您的刀片语法应为:

@if(count($errors) > 0)
      //your opening modal code goes here
@endif

It also depends on where you are loading your bootstrap JS files. 它还取决于您加载引导程序JS文件的位置。 If your bootstrap file is loaded after the above code then it will not load as the file is not loaded yet. 如果在上面的代码之后加载了引导文件,那么它将不会加载,因为文件尚未加载。

The simple option is to load your bootstrap JS files at the top of the body tag. 简单的选择是在body标签的顶部加载bootstrap JS文件。

The blade syntax that you're using is not correct (read the reference in https://laravel.com/docs/5.3/blade#if-statements ) And like #Amit said you got to make sure the javascript code to open your modal is loaded after the Bootstrap Js file is loaded, maybe using the $(document).ready() (refer to https://learn.jquery.com/using-jquery-core/document-ready/ ). 您正在使用的刀片语法不正确(请参阅https://laravel.com/docs/5.3/blade#if-statements中的参考资料)和#Amit一样,您必须确保使用javascript代码打开您的加载Bootstrap Js文件后加载modal,可能使用$(document).ready()(参见https://learn.jquery.com/using-jquery-core/document-ready/ )。

<script type="text/javascript">
    $(document).ready(function() {
        @if (count($errors) > 0)
            //show the modal
            $('#myModal').modal('show');
        @endif
    });
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM