简体   繁体   English

将javascript函数链接到Submit按钮

[英]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 它的工作原理以及使用scripthead

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM