JavaScript/jQuery - “$ 未定义 - $function()”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10779148/
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
JavaScript/jQuery - "$ is not defined- $function()" error
提问by CodingWonders90
I am trying to run a JavaScript/jQuery function and Firebuggets the error:
我正在尝试运行 JavaScript/jQuery 函数,Firebug收到错误消息:
$ is not defined $(function()".
The JavaScript code is placed inside a file called core.js
and referenced by index.php
. What causes this error?
JavaScript 代码放置在一个core.js
由调用和引用的文件中index.php
。是什么导致了这个错误?
JavaScript:
JavaScript:
<script type="text/javascript">
var formObject = {
run : function(obj) {
if (obj.val() === '') {
obj.nextAll('.update').html('<option value="">----</option>').attr('disabled', true);
} else {
var id = obj.attr('id');
var v = obj.val();
jQuery.getJSON('/mod/update.php', { id : id, value : v }, function(data) {
if (!data.error) {
obj.next('.update').html(data.list).removeAttr('disabled');
} else {
obj.nextAll('.update').html('<option value="">----</option>').attr('disabled', true);
}
});
}
}
};
$(function() {
$('.update').live('change', function() {
formObject.run($(this));
});
});
</script>
PHP/HTML
PHP/HTML
<html>
<select name="main" id="category" class="update">
<option value="">Select one</option>
<? if (!empty($list)) { ?>
<? foreach($list as $row) { ?>
<option value="<?php echo $row['id']; ?>">
<? echo $row['name']; ?>
</option>
<? } ?>
<? } ?>
</select>
</html>
回答by Ketan
You must not have made jQuery available to your script.
您一定没有让 jQuery 可用于您的脚本。
Add this to the top of your file:
将此添加到文件的顶部:
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
This issue is related to the jQuery/JavaScript file not added to the PHP/JSP/ASP file properly. This goes out and gets the jQuery code from the source. You could download that and reference it locally on the server which would be faster.
此问题与未正确添加到 PHP/JSP/ASP 文件的 jQuery/JavaScript 文件有关。这会从源代码中获取 jQuery 代码。您可以下载它并在服务器上本地引用它,这会更快。
Or either one can directly link it to jQuery or GoogleCDN or MicrosoftCDN.
或者可以直接将其链接到 jQuery 或 GoogleCDN 或 MicrosoftCDN。
回答by Fagner Brack
Try:
尝试:
(function($) {
$(function() {
$('.update').live('change', function() {
formObject.run($(this));
});
});
})(jQuery);
By using this way you ensure the global variable jQuery will be bound to the "$" inside the closure. Just make sure jQuery is properly loaded into the page by inserting:
通过使用这种方式,您可以确保全局变量 jQuery 将绑定到闭包内的“$”。只需通过插入以下内容来确保 jQuery 正确加载到页面中:
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
Replace "http://code.jquery.com/jquery-1.7.1.min.js" to the path where your jQuery source is located within the page context.
将“ http://code.jquery.com/jquery-1.7.1.min.js”替换为您的 jQuery 源在页面上下文中所在的路径。
回答by Loaderon
This may be useful to someone:
这可能对某人有用:
If you already got jQuery but still get this error, check you include jQuery before the js that uses it, specially if you use @RenderBody() in ASP.NET C#
如果您已经使用 jQuery 但仍然出现此错误,请检查您是否在使用它的 js 之前包含 jQuery,特别是如果您在 ASP.NET C# 中使用 @RenderBody()
You have to include jQuery before the @RenderBody() if you include the js inside the view that @RenderBody() calls.
如果在@RenderBody() 调用的视图中包含js,则必须在@RenderBody() 之前包含jQuery。
回答by lemon
You need to include the jQuery library on your page.
您需要在页面上包含 jQuery 库。
You can download jQuery hereand host it yourself or you can link from an external source like from Google or Microsoft.
您可以在此处下载 jQuery并自行托管,也可以从 Google 或 Microsoft 等外部资源链接。
Google's:
谷歌的:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
Microsoft's:
微软的:
<script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery/jquery-1.6.2.min.js">
回答by Shubham Paldewar
Include jquery.js and if it is included, load it before any other JavaScript code.
包含 jquery.js,如果包含,则在任何其他 JavaScript 代码之前加载它。
回答by Khachornchit Songsaen
I have solved it as follow.
我已经解决了它如下。
import $ from 'jquery';
(function () {
// ... code let script = $(..)
})();
回答by RichyRoo
Im using Asp.Net Core 2.2 with MVC and Razor cshtml My JQuery is referenced in a layout page I needed to add the following to my view.cshtml:
我在 MVC 和 Razor cshtml 中使用 Asp.Net Core 2.2 我的 JQuery 在布局页面中被引用,我需要将以下内容添加到我的 view.cshtml:
@section Scripts {
$script-here
}