jQuery不起作用

时间:2013-02-04 14:01:25

标签: jquery

当我提交表单时,无论我写的jquery代码如何,页面都会自动刷新。谁能指出我错在哪里?

我的html文件:

<html>
<head>
<script type="text/javascript" src="./js/up.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
</head>
<body>
<form id="up" action="" method="post">
<input id="image" type="text" name="file"/><input id="submit" type="submit"/>
</form>
</body>
</html>

我的javascript文件

$(document).ready(function(){
    $('input#submit').click(function(){
        alert('hi');
            /*some codes goes here*/
    });
});

当页面加载时,控制台显示此错误:Uncaught ReferenceError: $ is not defined即使我没有调用任何jquery函数。

2 个答案:

答案 0 :(得分:4)

你需要在jQuery之后包含你的jQuery依赖脚本:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript" src="./js/up.js"></script>

你应该只有一个jQuery文件。

答案 1 :(得分:2)

您需要删除第一个script到jquery,这是一个旧版本..

另外,我认为您的javascript位于up.js文件中。在jQuery之前请求该文件。您应首先加载jQuery,然后加载自定义代码..

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript" src="../js/up.js"></script>

您还将错误链接到up.js文件。您应该使用../代替./来转到父文件夹。