[英]Link javascript function to Submit button
This is the structure of my HTML: 这是我的HTML的结构:
<html>
<body>
<head>
...
<script type='text/javascript' src="js/javascript.js"></script>
</head>
...
<button class="submit" onclick="submitted()">Submit</button>
</body>
</html>
My file directory is Desktop/Project/
. 我的文件目录是Desktop/Project/
。 Within the project folder the HTML file is on its own and there is a subfolder called js
where javascript.js
exists. 在项目文件夹中,HTML文件是独立的,并且存在javascript.js
的子文件夹js
。
Within the external js file it goes something like this: 在外部js文件中,它是这样的:
function submitted(){
...
subtask(variable)
...
}
function subtask(param){
...
}
Within submitted()
I added alert('hello');
在submitted()
我添加了alert('hello');
as the first thing and it wouldn't trigger the alert when I refresh the html page. 作为第一件事,当我刷新html页面时,它不会触发警报。 Am I doing something wrong? 难道我做错了什么?
Change your <script>
tag to: 将您的<script>
标记更改为:
<script type="text/javascript" src="js/javascript.js"></script>
..
means "one directory above the current directory". ..
表示“当前目录上方的一个目录”。
According to your descriptions, the html file is within project folder and the js file is within project/js folder, then the src path ../js/javascript.js
must be wrong. 根据您的描述,html文件位于project文件夹中,而js文件位于project / js文件夹中,那么src路径../js/javascript.js
必须错误。 Try js/javascript.js
试试js/javascript.js
Originally, I thought it was because you put your script
tag within head
, not after button
tag. 最初,我认为这是因为您将script
标签放在head
内,而不是放在button
标签后。 But I tried it. 但是我尝试了。 It works well with script
in head
它的工作原理以及使用script
在head
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.