简体   繁体   中英

JavaScript function is not defined in laravel

This is my script containing the function which is not defined as per error stated.

 @section('script')
    <script src="{{ asset('datatable/jquery.dataTables.min.js')}}"></script>
    <script src="{{ asset('datatable/datatables.bootstrap4.min.js')}}"></script>
    <script type="text/javascript">
    function NewCategory(id = 0, type = "add") 
    {
        if (type == "add") {
            var url = "{{url('admin/categories/create')}}";
        } else {
            var url = "{{url('/admin/categories')}}" + "/" + id + "/edit";
        }  
        $("#myModalContenct").load(url, function(response, status, xhr) {
            if (status == "error") {
                var msg = "Sorry but there was an error: "; 
                ErrorAlert(msg + xhr.status + " " + xhr.statusText)
            } else { 
                $('#CategoryForm').submit(function(event) {
                    event.preventDefault();
                    SaveCategory();
                });
                $("#modal").modal("show");
            } 
        });
    }

here I called the function

    <button class="btn btn-sm btn-primary" onclick="NewCategory()">
        <i class="fas fa-plus-circle mr-2"></i>
        Add New Category
    </button> 

I am getting this error

categories:150 Uncaught ReferenceError: NewCategory is not defined at HTMLButtonElement.onclick (categories:150)

找到了解决方案...通过从 javascript 开头删除 @section('script') 和从 javascript 结尾删除 @stop,它起作用了....

on app.blade make

@yield('script2')

and use

@section('script2')
<script>
//CODE
</script>
@endsection

I think you use @section('script') twice

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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