JavaScript / jQuery - " $ is not defined- $ function()"错误 [英] JavaScript/jQuery - "$ is not defined- $function()" error

查看:157
本文介绍了JavaScript / jQuery - " $ is not defined- $ function()"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试运行JavaScript / jQuery函数并 Firebug 获取错误:

I am trying to run a JavaScript/jQuery function and Firebug gets the error:

$ is not defined $(function()".

JavaScript代码放在名为 core.js 的文件中,并由引用index.php 。导致此错误的原因是什么?

The JavaScript code is placed inside a file called core.js and referenced by index.php. What causes this error?

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

<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>


推荐答案

您的脚本不能使jQuery可用。

You must not have made jQuery available to your script.

将此项添加到您的文件顶部:

Add this to the top of your file:

<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>

此问题与未正确添加到PHP / JSP / ASP文件的jQuery / JavaScript文件有关。这样就可以从源代码中获取jQuery代码。你可以下载它并在服务器上本地引用它会更快。

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.

或者任何一个都可以直接将它链接到jQuery或GoogleCDN或MicrosoftCDN。

Or either one can directly link it to jQuery or GoogleCDN or MicrosoftCDN.

如何将jQuery添加到您的网页

这篇关于JavaScript / jQuery - &quot; $ is not defined- $ function()&quot;错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆