---
title: "document.getElementById(\"\")的一个奇怪问题"
author: "lndalian2000"
date: "2009-11-19T11:04:26Z"
tags: []
slug: "document-getelementbyid"
draft: false
lastmod: "2009-11-19T11:19:55Z"
id: 520180
url: "https://lndalian2000.iteye.com/blog/520180"
---

```
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

</head>
<body>


<form id="Searchid" name="Form1" onsubmit="return true;" action="Search.action" method="post">
<table width="100%" border="1">  

 <tr>
      	<td width="10%">1</td>
    	<td width="90%">
    		<form id="testform1" 
                          method="post" 
                          onsubmit="return false" 
                          action="toSelect.action">

    			<input type="hidden" name="id" value="4" />

    		</form>
<a href="#"   onclick="document.getElementById('testform1').submit();">
ID
</a>
    	</td>
</tr>

</table>
</form>


</body>
</html>
```

提示JS错误。

 

但是在修改成如下代码后就可以提交。

 

```
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

</head>
<body>


<form id="Searchid" name="Form1" onsubmit="return true;" action="Search.action" method="post">
	
	<form id="1234" name="1234" onsubmit="return false;"
	action="test.action" method="post">
	</form>
	
<table width="100%" border="1">  

 <tr>
      	<td width="10%">1</td>
    	<td width="90%">
    		<form id="testform1" 
                          method="post" 
                          onsubmit="return false" 
                          action="toSelect.action">

    			<input type="hidden" name="id" value="4" />

    		</form>
<a href="#"   onclick="document.getElementById('testform1').submit();">
ID
</a>
    	</td>
</tr>

</table>
</form>


</body>
</html>
```

 发现了没有。

 

加了一个

```
<form id="1234" name="1234" onsubmit="return false;"
	action="test.action" method="post">
</form>
```

 居然就好用了。

 

可是这是为什么呢？这个Form是没有用的。但是加了以后为什么。下面的那个Form就可以提交了。不出现JS错误了.

 

网上看到一篇文章：

 

document.getElementById("")为空或不是对象解决办法

网页设计中经常会使用此方法，当出现这个错误的时候，不妨使用alert();来调试调试。

alert(document.getElementById("name"));

看看是否有弹出框:

1，若无弹出框，则说明不存在该ID=name。

2，若有弹出框，则说明存在该ID=name，那么就可能是因为改脚本语言所处在网页文本位置在ID=name标签上部，把脚本写到该HTML标签下面试验。即可得到该对象值。

 

但是好像还是说明不了上面的情况。

 

 

![](/images/smiles/icon_mad.gif)