Javascript";变量未定义";

Javascript";变量未定义";,javascript,html,Javascript,Html,我正在编写一个简单的javascript函数,它从三个文本区域中获取信息。非常基本,但出于某种原因,javascript告诉我我的变量没有定义,我不明白为什么 html如下所示: <input type = "text" id = "month" name = "month"/> <input type = "text" id = "day" name = "day"/> <input type = "text" id = "year" name =

我正在编写一个简单的javascript函数,它从三个文本区域中获取信息。非常基本,但出于某种原因,javascript告诉我我的变量没有定义,我不明白为什么

html如下所示:

<input type = "text" id = "month" name = "month"/>
    <input type = "text" id = "day" name = "day"/>
    <input type = "text" id = "year" name = "year"/>
    <input type = "button" id = "submit" value = "Go!" onClick = "calculate(month, day, year)"/>
当我运行这个并点击我的按钮时,它在Firebug中显示“未定义月份”。所以我有点困惑,因为。。。他们被定义了


帮忙?谢谢

被解释为不存在的变量名。要传递字符串:

onClick = "calculate('month', 'day', 'year')"/>

您没有定义它们:要做到这一点,您将:

calculate('a', 'b', 'c');
在您的情况下,您需要大量更改代码或更改函数的功能:

我想说,做选择2:

onclick ="calculate(document.getElementById('month'), document.getElementById('day'), document.getElementById('year'));"
功能包括:

function calculate(month, day, year){
   var setMonth = month.value;
   var setDay = day.value;
   var setYear = year.value;
}
在html的“onClick”部分,您试图发送尚未定义的“month”、“day”和“year”变量


不要传递变量,试着只运行“calculate();”函数,从函数内的表单中获取数据。

调用函数时,必须传递参数


Javascript代码:

函数计算(月、日、年){
var setMonth=month.value;
var setDay=day.value;

var setYear=year.value;}

请注意,calculate函数没有任何参数,并且 根据字段的名称查询文档

HTML


如果需要,请使用函数$(document).ready(…)

您需要在
标记中添加以下行:


<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>


我比我更喜欢你的答案,但我想我应该传递值,而不是在函数中计算它-这样,函数可以用于表单,或者只是传递数字。@Dave我只是想保持OP完全理解的相同格式。就像我说的,我更喜欢你的答案:)只是做笔记,以防OP使用你的答案——提供一个小的改变。如果一个关于javascript的问题没有提到jQuery,那么这个问题的结果如何?此外,您不必把jQuery放在头上,最好将它(以及所有其他可能的脚本)放在页面的末尾。
<input type = "text" id = "month" name = "month"/>
    <input type = "text" id = "day" name = "day"/>
    <input type = "text" id = "year" name = "year"/>
    <input type = "button" id = "submit" value = "Go!" onClick = "calculate()"/>
function calculate(){
var setMonth = document.getElementById('month').value;
var setDay = document.getElementById('day').value;
var setYear = document.getElementById('year').value;}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>