简体   繁体   中英

Link javascript function to Submit button

This is the structure of my 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/ . Within the project folder the HTML file is on its own and there is a subfolder called js where javascript.js exists.

Within the external js file it goes something like this:

function submitted(){
...
subtask(variable)
...
}

function subtask(param){
...
}

Within submitted() I added alert('hello'); as the first thing and it wouldn't trigger the alert when I refresh the html page. Am I doing something wrong?

Change your <script> tag to:

<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. Try js/javascript.js

Originally, I thought it was because you put your script tag within head , not after button tag. But I tried it. It works well with script in head

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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