未捕获的 ReferenceError:$ 未在 Laravel 中的 http://localhost/project/public/user/1/edit:308:9 处定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40801136/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:48:45  来源:igfitidea点击:

Uncaught ReferenceError: $ is not defined at http://localhost/project/public/user/1/edit:308:9 in Laravel

javascriptjquerylaravel

提问by Fauzi PadLaw

Jquery

查询

<script src="http://localhost/project/public/plugins/jQuery/jquery-2.2.3.min.js"></script>

my code

我的代码

<script type="text/javascript">
        function readURL(input){
          if(input.files && input.files[0]){
            var reader = new FileReader();
            reader.onload=function(e){
              $('#showimages').attr('src', e.target.result);
            }
            reader.readAsDataURL(input.files[0]);
          }
        }
        $("#inputimages").change(function(){
          readURL(this);
        });
        </script>


        <img src="" id="showimages">
        <input type="file" name="images" id="inputimages">

and i got this error

我得到了这个错误

Uncaught ReferenceError: $ is not defined at http://localhost/project/public/user/1/edit:308:9

未捕获的 ReferenceError:$ 未在http://localhost/project/public/user/1/edit:308:9 处定义

*line 308 -> $("#inputimages").change(function(){

*第308行-> $("#inputimages").change(function(){

Im new in js and jquery, Help me solve this error please...

我是 js 和 jquery 的新手,请帮我解决这个错误...

I wrote the script out of the section. That was why the error occurred, see my answer.

我写了这个部分的脚本。这就是发生错误的原因,请参阅我的答案。

回答by Fauzi PadLaw

Thankyou for everyone!

谢谢大家!

This error is solved, in .blade.php laravel i have to write

这个错误解决了,在.blade.php laravel我必须写

@section('script')
<script type="text/javascript">
//my code here
</script>
@endsection

回答by Natsathorn

To use Jquery, remember it has to import before using

要使用Jquery,记住它必须在使用前导入

Your problem is look like your Jquery is not imported. Maybe your path is incorrect or you imported it after your script.

您的问题是您的 Jquery 未导入。也许您的路径不正确,或者您在脚本之后导入了它。

First, check this url http://localhost/project/public/plugins/jQuery/jquery-2.2.3.min.jswith with your web browser. If it exist you will see a lot of javascript code there.

首先,使用您的网络浏览器检查这个 url http://localhost/project/public/plugins/jQuery/jquery-2.2.3.min.js。如果它存在,你会在那里看到很多 javascript 代码。

Then to import it from your local server.

然后从本地服务器导入它。

<script src="http://localhost/project/public/plugins/jQuery/jquery-2.2.3.min.js"></script>
<script>
      //your script here.
</script>

Another choice using cdn instead:

使用 cdn 的另一种选择:

<script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
<script>
     //your script here
</script>

回答by Vahan Nasibyan

  1. You have JavaScript running before the page is fully loaded.
  2. You can load another plugin which may have overwritten the $ variable.
  3. Your JavaScript file is not being properly loaded into your page(Check you script path and try to open link directly on browser).
  1. 在页面完全加载之前,您已运行 JavaScript。
  2. 您可以加载另一个可能覆盖 $ 变量的插件。
  3. 您的 JavaScript 文件没有正确加载到您的页面中(检查您的脚本路径并尝试直接在浏览器上打开链接)。

Make sure all javascript code is being run inside a code block such as:

确保所有 javascript 代码都在代码块中运行,例如:

$(document).ready(function () {
  //your code
});

or

或者

(function($){ })(jQuery);

or equivalent pure javascript code.

或等效的纯 javascript 代码。

回答by Alexey Mezenin

Looks like jQuery wasn't loaded when your script is exectued.

执行脚本时似乎未加载 jQuery。

You should load jQuery before scripts that are using it.

您应该在使用它的脚本之前加载 jQuery。

回答by 502_Geek

There may be two potential problem in your code.

您的代码中可能存在两个潜在问题。

  1. You don't have to put your code before scripts that using it. (Check your view source from browser)
  2. You may imported another jQuery library.
  1. 您不必将代码放在使用它的脚本之前。(从浏览器检查您的视图源)
  2. 您可能导入了另一个 jQuery 库。

If you imported two different jQuery Library, you can solve with noConflict()

如果你导入了两个不同的 jQuery 库,你可以用 noConflict()

e.g

例如

  <script type="text/javascript">
    j = $.noConflict();
    function readURL(input){
      if(input.files && input.files[0]){
        var reader = new FileReader();
        reader.onload=function(e){
          j('#showimages').attr('src', e.target.result);
        }
        reader.readAsDataURL(input.files[0]);
      }
    }
    j("#inputimages").change(function(){
      readURL(this);
    });
  </script>

Try my updated answer. Assumed you have main layout file.

试试我更新的答案。假设您有主布局文件。

<head>
 <script src="{!! asset('plugins/jQuery/jquery-2.2.3.min.js') !!}"></script> 
 <script src="{!! asset('bootstrap/js/bootstrap.min.js') !!}"></script>
 <script src="{!! asset('plugins/select2/select2.full.min.js') !!}">    </script> 
 <script src="{!! asset('plugins/slimScroll/jquery.slimscroll.min.js') !!}"></script> 
 <script src="{!! asset('plugins/fastclick/fastclick.js') !!}"></script>     
 @yield('script')

<body>
    @yield('content')
     <script src="{!!asset('plugins/datatables/jquery.dataTables.min.js')!!}"></script> 
</body>
 </head>