I am trying to run a JavaScript/jQuery function and Firebug gets 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>
5 个解决方案
#1
48
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。
How do add jQuery to your webpage
如何将jQuery添加到您的网页
#2
6
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源在页面上下文中所在的路径。
#3
2
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。
#4
2
You need to include the jQuery library on your page.
您需要在页面上包含jQuery库。
You can download jQuery here and 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">
#5
1
Include jquery.js and if it is included, load it before any other JavaScript code.
包含jquery.js,如果包含它,请在任何其他JavaScript代码之前加载它。
#1
48
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。
How do add jQuery to your webpage
如何将jQuery添加到您的网页
#2
6
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源在页面上下文中所在的路径。
#3
2
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。
#4
2
You need to include the jQuery library on your page.
您需要在页面上包含jQuery库。
You can download jQuery here and 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">
#5
1
Include jquery.js and if it is included, load it before any other JavaScript code.
包含jquery.js,如果包含它,请在任何其他JavaScript代码之前加载它。