Javascript “document.formName”未定义?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7436277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 02:16:21  来源:igfitidea点击:

"document.formName" is undefined?

javascriptasp.net

提问by voithos

I've got an ASP.NET site running locally on my XP system using IIS Express. There is a live version of the site running on a Windows server.

我有一个使用 IIS Express 在我的 XP 系统上本地运行的 ASP.NET 站点。该站点有一个在 Windows 服务器上运行的实时版本。

In the web pages, often the Javascript will reference a form on the page using the style document.formName, where formNameis the name of the form. As far as I know, this is a cross-browser method, along with document.forms.formNameand document.forms[0]and so forth.

在网页中,Javascript 通常会使用 style 引用页面上的表单document.formName,其中formName是表单的名称。据我所知,这是一个跨浏览器的方法,随着document.forms.formNamedocument.forms[0]等等。

On my local development site, the reference document.frm1(I know, bad naming practice) errors out; it is undefined. On the other hand, document.forms.frm1works fine. Strangely, this doesn't occur on the server, although both pages are identical as far as the code goes. I've double checked with Firebug, and in both IE8 and Firefox 6.

在我的本地开发站点上,参考document.frm1(我知道,糟糕的命名习惯)出错了;它是未定义的。另一方面,document.forms.frm1工作正常。奇怪的是,这不会发生在服务器上,尽管就代码而言,这两个页面是相同的。我已经仔细检查了 Firebug,以及 IE8 和 Firefox 6。

Another weird part: checking with Firebug, document.frm1is undefined, but document.frmClose(another form) exists! Huh?!

另一个奇怪的部分:用 Firebug 检查,document.frm1未定义,但document.frmClose(另一种形式)存在!嗯?!

Anyone experienced this before?

有没有人经历过这种情况?

回答by Chris Baker

EDIT 1

编辑 1

I did some experimentation, and found that the idproperty is used for document.forms, whereas the nameproperty seems to be used for document.formName

我做了一些实验,发现该id属性用于document.forms,而该name属性似乎用于document.formName

http://jsfiddle.net/GVjsv/

http://jsfiddle.net/GVjsv/



Original Answer

原答案

Ensure that your javascript is not executing before DOM is ready. One way to help is to put your javascript at the bottom of the page, or if you're using a framework be sure to wrap the code in a ready-type function:

确保在 DOM 准备好之前您的 javascript 没有执行。一种帮助方法是将您的 javascript 放在页面底部,或者如果您使用的是框架,请确保将代码包装在ready-type 函数中:

jQuery: http://api.jquery.com/ready/

jQuery:http: //api.jquery.com/ready/

Mootools: http://mootools.net/docs/core/Utilities/DomReady

Mootools:http: //mootools.net/docs/core/Utilities/DomReady

Vanilla javascript:

香草javascript:

window.onload = function() {
  // Code to be run.
} 

The reason that this is inconsistent between servers could be that the local development server loads the page more quickly than the live server does. The timing works out so that you usuallydon't get the same error in both places - the emphasis is added because if you tried enough times, you would probably be able to reproduce the error in both places.

服务器之间不一致的原因可能是本地开发服务器比实时服务器加载页面的速度更快。时间安排好,因此您通常不会在两个地方遇到相同的错误 - 增加了重点,因为如果您尝试了足够多的时间,您可能能够在两个地方重现错误。

回答by Robert Brooker

To get the nameattribute added to your formelement you need to add...

要将name属性添加到您的表单元素中,您需要添加...

<system.web>
    <pages controlRenderingCompatibilityVersion="3.5" /> <!-- http://www.asp.net/whitepapers/aspnet4/breaking-changes -->
</system.web>

...to your web.config

...到您的web.config

回答by Asmor

I recommend not using the name attribute for JavaScript identification purposes. Instead, give the element an id and use

我建议不要将 name 属性用于 JavaScript 识别目的。相反,给元素一个 id 并使用

document.getElementById("elementId");

e.g.

例如

<form id="form1"></form>
<script>
  var form = document.getElementById("form1");
</script>

回答by Nitin Shah

The best solution for this is below:
Just add a name property in form tag


If you are using below code

最好的解决方案如下:
只需在表单标签中添加一个名称属性


如果您使用以下代码

<form id="form1"></form>
<script>
  var form = document.form1; //getting undefined error here
</script>

What you need to perform here is:
add "name" property in form tag like below

您需要在这里执行的是:
在表单标签中添加“名称”属性,如下所示

<form id="form1" name="form1"></form>
<script>
var form = document.form1; //Now it will work fine and provide the correct value
</script>