跨浏览器问题 - IE无法找到动态表单 [英] Cross Browser Problem - IE can not find a dynamic form

查看:77
本文介绍了跨浏览器问题 - IE无法找到动态表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人知道为什么IE会给出错误"'''document.myForm.dummy''

null或者不是对象"在下一页? Firefox做了我想要的b $ b,创建一个文本框并写入你好。在它里面。


< html>

< head>

< SCRIPT type =" text / javascript" /">

函数createForm(){


var divEle = document.getElementById(" putHere");

var formEle = document.createElement(" form");

formEle.setAttribute(" name"," myForm");

formEle.setAttribute(" ;方法"," post");

formEle.setAttribute(" action"," page.jsp");

divEle.appendChild(formEle);


var dummyEle = document.createElement(" input");

dummyEle.setAttribute(" type"," text");

dummyEle.setAttribute(" name"," dummy");

formEle.appendChild(dummyEle);


}

< / script>

< / head>

< body>

< div id = " putHere">< / div>

< script ty pe =" text / javascript">

createForm();

document.myForm.dummy.value =" hello";

< / script>

< / body>

< / html>

Does anyone know why IE gives the error "''document.myForm.dummy'' is
null or not an object" on the following page? Firefox does what I
expect, creates a text box and writes "hello" in it.

<html>
<head>
<SCRIPT type="text/javascript"/">
function createForm(){

var divEle = document.getElementById("putHere");
var formEle = document.createElement("form");
formEle.setAttribute("name", "myForm");
formEle.setAttribute("method", "post");
formEle.setAttribute("action", "page.jsp");
divEle.appendChild(formEle);

var dummyEle = document.createElement("input");
dummyEle.setAttribute("type", "text");
dummyEle.setAttribute("name", "dummy");
formEle.appendChild(dummyEle);

}
</script>
</head>
<body>
<div id="putHere"></div>
<script type="text/javascript">
createForm();
document.myForm.dummy.value = "hello";
</script>
</body>
</html>

推荐答案

BTW:如果表格没有动态创建,它就有效。


< html>

< head>

< / head>

< body>

< form name =" myForm">

< ; input type =" text" name =" dummy">

< / form>

< script type =" text / javascript">

document.myForm.dummy.value =" hello";

< / script>

< / body>

< / html>

BTW: It works if the form is not dynamicly created.

<html>
<head>
</head>
<body>
<form name="myForm">
<input type="text" name="dummy">
</form>
<script type="text/javascript">
document.myForm.dummy.value="hello";
</script>
</body>
</html>


ja **** @ gmail.com 写道:
有没有人知道为什么IE会给出错误"'''document.myForm.dummy''是
null或者不是对象"在下一页?
Does anyone know why IE gives the error "''document.myForm.dummy'' is
null or not an object" on the following page?




我从来没有调查过足以弄清IE中这个问题的真正根本原因。但IE肯定在按名称访问

动态创建的输入时遇到问题。另外,请参阅下面的示例,其中

说明更改输入的名称并不能在

下使用新名称,但它仍然可用以旧名称:


< form action =" /">

< input name =" test">

< / form>

< script type =" text / javascript">

var i = document.forms [0]。元素[''test''];

alert(i);

i.name =" junk";

alert(i .name);

alert(document.forms [0] .elements [''junk'']);

alert(document.forms [0] .elements [ ''test'']);

< / script>


-

Matt Kruse
< a rel =nofollowhref =http://www.JavascriptToolbox.comtarget =_ blank> http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com


ja **** @ gmail.com 于19/04/2006 10:47 AM AEST:
ja****@gmail.com said on 19/04/2006 10:47 AM AEST:
有谁知道为什么IE给出错误''document.myForm.dummy''是
null或不是对象在下一页? Firefox做我所期望的,创建一个文本框并写入你好。在里面。


因为IE坏了,所以不允许你为一个荒谬的原因添加一个名称属性到一个动态生成的形式

。见下文。


< html>
< head>
< SCRIPT type =" text / javascript" /">
-------------------------------- ^^


一个HTML标记错误,似乎不会影响结果。


函数createForm(){

var divEle = document.getElementById(" putHere" ;);
var formEle = document.createElement(" form");
formEle.setAttribute(" name"," myForm");
Does anyone know why IE gives the error "''document.myForm.dummy'' is
null or not an object" on the following page? Firefox does what I
expect, creates a text box and writes "hello" in it.
Because IE is broken, it doesn''t let you add a name attribute to a
dynamically generated form for some absurd reason. See below.

<html>
<head>
<SCRIPT type="text/javascript"/"> --------------------------------^^

An HTML markup error that doesn''t seem to affect the outcome.

function createForm(){

var divEle = document.getElementById("putHere");
var formEle = document.createElement("form");
formEle.setAttribute("name", "myForm");



您可以添加ID,然后使用完全指定的属性名称:


alert(typeof document.forms [''myForm'']);

输入元素相同。


而不是setAttribute,在某些情况下在IE中被破坏,

直接访问属性(非标准但更好的支持):


formEle.name =" myForm" ;;

formEle.id =" myForm";

...


dummyEle.name =" dummy" ;;

dummyEle.id =" dummy" ;;

节省了ty ping也。 :-)


现在你使用以下内容将值放入输入:


document.forms [''myForm'']。elements [ ''dummy'']。value =" hello";

[...]

-

Rob

小组常见问题:<网址:http://www.jibbering.com/FAQ>



You can add an ID, then use the fully specified property name:

alert( typeof document.forms[''myForm''] );
Same for the input element.

Rather than setAttribute, which is broken in IE for some situations,
access properties directly (non-standard but better supported):

formEle.name = "myForm";
formEle.id = "myForm";

...

dummyEle.name = "dummy";
dummyEle.id = "dummy";
Saves on typing too. :-)

Now you put the value into the input using:

document.forms[''myForm''].elements[''dummy''].value = "hello";
[...]
--
Rob
Group FAQ: <URL:http://www.jibbering.com/FAQ>


这篇关于跨浏览器问题 - IE无法找到动态表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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