简体   繁体   English

如何在 html 中使用 javascript 函数?

[英]How do I use javascript functions in html?

I have been trying to use a function in a javascript file inside another html file.我一直在尝试在另一个 html 文件中的 javascript 文件中使用一个函数。 Essentially, it's not working and I think it has to do with my trying to make the javascript accessible on the html file.从本质上讲,它不起作用,我认为这与我试图使 html 文件上的 javascript 可访问有关。 The function is supposed to add the values taken from the form and then output that sum.该函数应该将从表单中获取的值相加,然后输出该总和。 The first bit of code is my .js file and the second is my html.代码的第一位是我的 .js 文件,第二位是我的 html。

 function reset() { var q = new Array(5); for(var i = 0; i < 5; i++) { q[i] = 100; } } function calculate(form) { var q = new Array(5); for(var i = 0; i < 5; i++) { q[i] = 100; } q[1] = document.worksheet1.Q1.options[document.worksheet1.Q1.selectedIndex].value; q[2] = document.worksheet1.Q2.options[document.worksheet1.Q2.selectedIndex].value; q[3] = document.worksheet1.Q3.options[document.worksheet1.Q3.selectedIndex].value; q[4] = document.worksheet1.Q4.options[document.worksheet1.Q4.selectedIndex].value; q[5] = document.worksheet1.Q5.options[document.worksheet1.Q5.selectedIndex].value; var total = (q[1]*1.0)+(q[2]*1.0)+(q[3]*1.0)+(q[4]*1.0)+(q[5]*1.0); document.worksheet1.totals.value = total; }
 <html> <header> <script language="Javascript" src="tmdcalculation.js" type="text/javascript"></script> </header> <body> <form name="worksheet1" id="worksheet1" onsubmit="calculate"> <table border="1" align="center"> <tr> <td><table> <tr> <td>Feeling</td> <td>How I have felt</td> </tr> <tr> <td>Question 1?</td> <td><select name="Q1" size="1"> <option value="0" selected="selected">Not at all</option> <option value="1">A Little</option> <option value="2">Moderately</option> <option value="3">Quite a Bit</option> <option value="4">Extremely</option> </select></td> </tr> <tr> <td>Question 2?</td> <td><select name="Q2" size="1"> <option value="0" selected="selected">Not at all</option> <option value="1">A Little</option> <option value="2">Moderately</option> <option value="3">Quite a Bit</option> <option value="4">Extremely</option> </select></td> </tr> <tr> <td>Question 3?</td> <td><select name="Q3" size="1"> <option value="0" selected="selected">Not at all</option> <option value="1">A Little</option> <option value="2">Moderately</option> <option value="3">Quite a Bit</option> <option value="4">Extremely</option> </select></td> </tr> <tr> <td>Question 4?</td> <td><select name="Q4" size="1"> <option value="0" selected="selected">Not at all</option> <option value="1">A Little</option> <option value="2">Moderately</option> <option value="3">Quite a Bit</option> <option value="4">Extremely</option> </select></td> </tr> <tr> <td>Question 5?</td> <td><select name="Q5" size="1"> <option value="0" selected="selected">Not at all</option> <option value="1">A Little</option> <option value="2">Moderately</option> <option value="3">Quite a Bit</option> <option value="4">Extremely</option> </select></td> </tr> </table></td> </tr> </table> <table border="1" align="center"> <tr> <td><table> <tr> <td align="right"><input name="button" type="button" onclick="calculate(this.form)" value="Analyse" /></td> <td>Total Mood Calc: <input name="totals" type="text" size="3" /></td> <td><input name="reset" type="reset" onclick="initial()" value="Reset" /></td> </tr> </table></td> </tr> </table> </form> </body> </html>

Ps Some extra questions. Ps 一些额外的问题。 I had borrowed some of the code from a website because I am still quite new to both javascript and html.我从网站上借用了一些代码,因为我对 javascript 和 html 仍然很陌生。 Why does the reset function have the array values all turned to 100?为什么reset函数的数组值都变成了100? And, why doesn't the calculate function just call the reset function instead of doing the same thing?而且,为什么calculate 函数不直接调用reset 函数而不是做同样的事情呢?

I'm only a newbie to jscript and this may not work but can give this a go我只是 jscript 的新手,这可能行不通,但可以试一试

function reset()
{
    var q = new Array(5);

    for(var i = 0; i < 5; i++)
    {
        q[i] = 100;
    }
}

function calculate() {

    var q = new Array(5);
    for(var i = 0; i < 5; i++) {
        q[i] = 100;
    }
    q[1] = document.worksheet1.Q1.options[document.worksheet1.Q1.selectedIndex].value;
    q[2] = document.worksheet1.Q2.options[document.worksheet1.Q2.selectedIndex].value;
    q[3] = document.worksheet1.Q3.options[document.worksheet1.Q3.selectedIndex].value;
    q[4] = document.worksheet1.Q4.options[document.worksheet1.Q4.selectedIndex].value;
    q[5] = document.worksheet1.Q5.options[document.worksheet1.Q5.selectedIndex].value;

    var total = (q[1]*1.0)+(q[2]*1.0)+(q[3]*1.0)+(q[4]*1.0)+(q[5]*1.0);

    document.worksheet1.totals.value = total;
}

<form name="worksheet1" id="worksheet1" onsubmit="return calculate()"> 

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

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