继续得到未定义的变量错误 [英] Keep getting undefined variable error

查看:71
本文介绍了继续得到未定义的变量错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!


关于如何获取脚本错误的任何想法未定义

变量" Exp_Month"?


我试图让这个下拉列表默认为当前月份

而不是01.


谢谢你提前。


< SELECT size =" 1" name =" Exp_Month">

< OPTION value =" 01"选择> 1< / OPTION>

< OPTION value =" 02"> 2< / OPTION>

< OPTION value =" 03"> 3< / OPTION>

< OPTION value =" 04"> 4< / OPTION>

< OPTION value =" 05"> 5< / OPTION>

< OPTION value =" 06"> 6< / OPTION>

< OPTION value =" 07"> 7< / OPTION> ;

< OPTION value =" 08"> 8< / OPTION>

< OPTION value =" 09"> 9< / OPTION>

< OPTION value =" 10"> 10< / OPTION>

< OPTION value =" 11"> 11< / OPTION>

< OPTION value =" 12"> 12< / OPTION>

< / SELECT>


<脚本类型=" text / javascript">

var now = new Date()

Exp_Month.selectedIndex = now.getMonth()

< ; / script>

Hi!

Any ideas as to how I can get ride of the script error undefined
variable "Exp_Month"?

I am trying to get this drop down list to default to the current month
as opposed to 01.

Thank you in advance.

<SELECT size="1" name="Exp_Month">
<OPTION value="01" selected>1</OPTION>
<OPTION value="02">2</OPTION>
<OPTION value="03">3</OPTION>
<OPTION value="04">4</OPTION>
<OPTION value="05">5</OPTION>
<OPTION value="06">6</OPTION>
<OPTION value="07">7</OPTION>
<OPTION value="08">8</OPTION>
<OPTION value="09">9</OPTION>
<OPTION value="10">10</OPTION>
<OPTION value="11">11</OPTION>
<OPTION value="12">12</OPTION>
</SELECT>

<script type="text/javascript">
var now = new Date()
Exp_Month.selectedIndex = now.getMonth()
</script>

推荐答案

se ********* @ cfl.rr.com (查克)写在

news:85 ************************** @ posting.google.c om:
se*********@cfl.rr.com (Chuck) wrote in
news:85**************************@posting.google.c om:
< SELECT size =" 1" name =" Exp_Month">
< OPTION value =" 01"选择> 1< / OPTION>
< OPTION value =" 02"> 2< / OPTION>
< OPTION value =" 03"> 3< / OPTION>
< OPTION value =" 04"> 4< / OPTION>
< OPTION value =" 05"> 5< / OPTION>
< OPTION value =" 06" > 6< / OPTION>
< OPTION value =" 07"> 7< / OPTION>
< OPTION value =" 08"> 8< / OPTION> < OPTION value =" 09"> 9< / OPTION>
< OPTION value =" 10"> 10< / OPTION>
< OPTION value =" 11" > 11< / OPTION>
< OPTION value =" 12"> 12< / OPTION>
< / SELECT>

< script type =" ; text / javascript">
var now = new Date()
Exp_Month.selectedIndex = now.getMonth()
< / script>
<SELECT size="1" name="Exp_Month">
<OPTION value="01" selected>1</OPTION>
<OPTION value="02">2</OPTION>
<OPTION value="03">3</OPTION>
<OPTION value="04">4</OPTION>
<OPTION value="05">5</OPTION>
<OPTION value="06">6</OPTION>
<OPTION value="07">7</OPTION>
<OPTION value="08">8</OPTION>
<OPTION value="09">9</OPTION>
<OPTION value="10">10</OPTION>
<OPTION value="11">11</OPTION>
<OPTION value="12">12</OPTION>
</SELECT>

<script type="text/javascript">
var now = new Date()
Exp_Month.selectedIndex = now.getMonth()
</script>




我找到的最简单的方法是将脚本更改为:


< script type =" text / javascript"&g t;

var now = new Date();

document.getElementById(''Exp_Month'')。selectedIndex = now.getMonth();

< / script>

或者,为表格命名。如果命名为form_name,则通过以下方式调用元素:

document.form_name.Exp_Month。 selectedIndex = now.getMonth();


最后一件事,你可能知道。 。 。

请注意,月份为零,1月为零,2月份为1,

等。因此调用元素的selectedIndex有效,因为选项是

零基础喜欢这几个月。但是,您的值为1-12。如果您计划

来使用元素的值,您可能需要将它们重新编号为0-11。

如果您打算使用价值



未来的月份相关/日期相关脚本功能的信息。


祝你好运,
LJL



The easiest way I have found, is to change the script to:

<script type="text/javascript">
var now = new Date();
document.getElementById(''Exp_Month'').selectedIndex = now.getMonth();
</script>
Alternatively, name the form. If named "form_name", call the element by:

document.form_name.Exp_Month. selectedIndex = now.getMonth();

One last thing, which you probably know . . .
Be aware that months are zero-based, January is zero, Februaryis one,
etc. So calling the element''s selectedIndex works, because the options are
zero-based liked the months. However, your values are 1-12. If you plan
to use the element''s values, you may want to renumber them 0-11.
It only makes a difference if you are going to use the value
information for some month-related/date related script function in the
future.

Good luck,
LJL


2004年2月9日星期一04:51:46 GMT,LJL< no ** @ nowhere.com>写道:


[snip]
On Mon, 09 Feb 2004 04:51:46 GMT, LJL <no**@nowhere.com> wrote:

[snip]
我找到的最简单的方法是将脚本更改为:
< script type =" text / javascript">
var now = new Date();
document.getElementById(''Exp_Month'')。selectedIndex = now.getMonth();
The easiest way I have found, is to change the script to:

<script type="text/javascript">
var now = new Date();
document.getElementById(''Exp_Month'').selectedIndex = now.getMonth();




请原谅?您不能将document.getElementById与名为

的元素一起使用。是的,它适用于IE,但它不适用于所有其他浏览器。

document.getElementById应与ID''d元素一起使用。如果你想使用DOM获取一个命名元素,请使用document.getElementsByName。


如果你试图访问表单中的命名元素,使用[1]:


document.formName.Exp_Month.selectedIndex


如果你不使用表格,请选择选择元素。


请注意,所有浏览器都不支持(或根本不支持)DOM。

您应该使用特征检测来确定对特定的支持

方法。下面的示例假设您为元素Exp_Month ID。


var expMonth = null;


if(document.getElementById){

expMonth = document.getElementById(''Exp_Month'');

}否则if(document.all){

expMonth = document。所有[''Exp_Month''];

}


expMonth.selectedIndex = .....


[snip]


Mike

[1]您还可以使用面向集合的访问器方法。它稍微有点b / b
可以用来访问ID''d元素,而

元素的名称包含被解释为
运算符(+, - ,[,],*等)。同样的表达式将是

写成:


document.forms [''formName'']。elements [''Exp_Month'']。selectedIndex


-

Michael Winter
M.******@blueyonder.co.inva 盖子(将.invalid替换为.uk以回复)



I beg your pardon? You cannot use document.getElementById with a named
element. Yes, it works in IE, but it won''t work in all the other browsers.
document.getElementById should be used with ID''d elements. If you want to
get a named element using the DOM, use document.getElementsByName.

If you are trying to access a named element within a form, use[1]:

document.formName.Exp_Month.selectedIndex

If you don''t use a form, ID the select element.

Be aware that the DOM is not supported well (or at all) by all browsers.
You should use feature detection to determine support for a particular
method. The example below assumes you ID''d the element, Exp_Month.

var expMonth = null;

if( document.getElementById ) {
expMonth = document.getElementById( ''Exp_Month'' );
} else if( document.all ) {
expMonth = document.all[ ''Exp_Month'' ];
}

expMonth.selectedIndex = .....

[snip]

Mike
[1] You can also use a collection-oriented accessor method. It is slightly
slower and more to type, but can be used to access ID''d elements, and
elements with names that include characters that are interpreted as
operators in JavaScript (+, -, [, ], *, etc). The same expression would be
written as:

document.forms[''formName''].elements[''Exp_Month''].selectedIndex

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)


2004年2月9日星期一08:44:58 GMT,Michael Winter

< M。****** @ blueyonder.co.invalid>写道:


[snip]
On Mon, 09 Feb 2004 08:44:58 GMT, Michael Winter
<M.******@blueyonder.co.invalid> wrote:

[snip]
var expMonth = null;

if(document.getElementById){
expMonth = document.getElementById(''Exp_Month'');
} if if(document.all){
expMonth = document.all [''Exp_Month''];
}

expMonth.selectedIndex = ...
var expMonth = null;

if( document.getElementById ) {
expMonth = document.getElementById( ''Exp_Month'' );
} else if( document.all ) {
expMonth = document.all[ ''Exp_Month'' ];
}

expMonth.selectedIndex = ...




这个例子应该结束


if( expMonth){

expMonth.selectedIndex = ...

}


[snip]


Mike


-

Michael Winter
M。****** @ blueyonder.co.inva 盖子(将.invalid替换为.uk以回复)



This example should end

if( expMonth ) {
expMonth.selectedIndex = ...
}

[snip]

Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)


这篇关于继续得到未定义的变量错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆