LJLThe 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 arezero-based liked the months. However, your values are 1-12. If you planto 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 valueinformation for some month-related/date related script function in thefuture.Good luck,LJL 请原谅?您不能将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 namedelement. 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 toget 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.selectedIndexIf 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 particularmethod. 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 slightlyslower and more to type, but can be used to access ID''d elements, andelements with names that include characters that are interpreted asoperators in JavaScript (+, -, [, ], *, etc). The same expression would bewritten as:document.forms[''formName''].elements[''Exp_Month''].selectedIndex--Michael Winter M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply) 这个例子应该结束 if( expMonth){ expMonth.selectedIndex = ... } [snip] Mike - Michael Winter M。****** @ blueyonder.co.inva 盖子(将.invalid替换为.uk以回复)This example should endif( expMonth ) {expMonth.selectedIndex = ...}[snip]Mike--Michael Winter M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply) 这篇关于继续得到未定义的变量错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-15 23:45