普通网友 2018-03-28 16:23
浏览 133
已采纳

Laravel 5.6 - 我是否需要包含JQuery或者是否已经包含在app.js中?

I have created a Laravel 5.6 project on my local.

I have <script src="{{ asset('js/app.js') }}" defer></script> in <head>

In one of the views, I tried

<script type="text/javascript">

    jQuery(document).ready(function(){

        jQuery( "#ddtype" ).change(function() {
            alert( "Handler for .change() called." );
        });

    });

</script>

but it gives me jQuery is not defined error

If I try Bootstrap 4's collapse function, it works fine. Does that mean jQuery is already included?

I have done npm install and npm run dev

My resources/js/app.js requires bootstrap.js which looks like (not full code but top few lines) as below

window._ = require('lodash');
window.Popper = require('popper.js').default;

/**
 * We'll load jQuery and the Bootstrap jQuery plugin which provides support
 * for JavaScript based Bootstrap features such as modals and tabs. This
 * code may be modified to fit the specific needs of your application.
 */

try {
    window.$ = window.jQuery = require('jquery');

    require('bootstrap');
} catch (e) {}

Please advise. I have checked in different browsers to make sure no cache issues etc.

Update

My layouts/app.blade.php structure is like this -

<html>
<head>
....
<!-- Scripts -->
    <script src="{{ asset('js/app.js') }}" defer></script>
....
</head>
<body>

    <div id="app">

        @include('layouts.nav')

        <main class="py-4">
            @yield('content')
        </main>
    </div>

</body>
</html>

and myview.blade.php

@extends('layouts.app')

@section('content')
     .....
     .....
    <script type="text/javascript">

        jQuery(document).ready(function(){

            jQuery( "#ddtype" ).change(function() {
                alert( "Handler for .change() called." );
            });

        });

    </script>
@endsection
  • 写回答

1条回答 默认 最新

  • du4629 2018-03-28 18:13
    关注

    jQuery loaded via your app.js, which is only loaded once the page is ready because of the defer attribute in your script tag.

    Your inline script tag where you call jQuery(document).ready is loaded as the page renders, therefore executed before the app.js has been loaded. Hence the error, since jQuery is not yet loaded at that time.

    To fix it, simply remove the defer attribute from the script tag.

    The defer attribute is a boolean attribute.

    When present, it specifies that the script is executed when the page has finished parsing.

    For more information about the defer attribute: https://www.w3schools.com/tags/att_script_defer.asp

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题