getElementsByName未定义动态元素(单选按钮) [英] getElementsByName undefined for dynamic elements (radio buttons)

查看:59
本文介绍了getElementsByName未定义动态元素(单选按钮)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组动态创建的单选按钮,在

渲染之后我通过获取集合的长度来尝试循环这个集合,但是

我保持收到错误声明元素未定义。我正在使用

getElelementsByName因为这些是单选按钮,但似乎没有看到

动态元素!!!


这是我的代码...请告诉我,如果有任何问题,我会做错了! - 谢谢

----

....

....

//这个循环通过一个ajax结构---这是正确的所以我知道

不是Ajax问题。


for(var i = 0; i< ds。 Tables [0] .Rows.length; i ++){

//创建nue输入元素

var r = document.createElement(''< INPUT type = radio name =公司>'');

r.type =" radio";

r.name =" company";

r.id =" myradiobutton" + i;

r.setAttribute(" value",ds.Tables [0] .Rows [i] .roottype);

td_company.appendChild(r);

td_company.appendChild(document.createTextNode(ds。Tables [0] .Rows [i] .roottypedesc));

}


-----

在渲染

radion按钮后调用我的函数试图获得该组的长度。如果我找到一个具有特定值的那个,

我检查它。


for(var n = 0; n< document.getElementsByName(" company") ;)。长度; n ++){

if(document.getElementsByName(" company")[n] .value ==''msde'')

{

document.getElementsByName(" country")[n] .checked =" checked";

}

}

解决方案

Saulaécrit:

我有一组在
渲染后动态创建的单选按钮我通过获取集合的长度来尝试循环通过此集合,但是
我不断收到错误,指出元素未定义。我正在使用
getElelementsByName因为这些是单选按钮,但似乎没有看到动态元素!!!

这是我的代码...请让我知道如果有什么我做错了! - 谢谢
----
...
...
//通过ajax结构循环---这是正确的所以我知道是
不是Ajax问题。

for(var i = 0; i< ds.Tables [0] .Rows.length; i ++){
//创建nue输入元素
var r = document.createElement(''< INPUT type = radio name = company>'');


var r = document.createElement(''INPUT'');

r.type =" radio";
r.name =" company";
r.id =" myradiobutton" + i;
r.setAttribute(" value",ds.Tables [0] .Rows [i] .roottype);


roottype不是Java语言吗?

td_company.appendChild(r);


什么是td_company?

td_company.appendChild(document.createTextNode(ds.Tables [0] .Rows [i] .roottypedesc));
}

-----
在渲染
radion按钮后调用我的函数试图获得集合的长度。如果我找到一个具有特定值的那个,我会检查它。

for(var n = 0; n< document.getElementsByName(" company")。length; n ++){
if(document.getElementsByName(" company")[n] .value ==''msde'')
{
document.getElementsByName(" country")[n]。 checked =" checked";
}
}




var R = document.getElementsByName(" company");

var C = document.getElementsByName(" country");

for(var n = 0; n< R.length; n ++)

if(b R [n] .value ==''msde'')C [n] .checked =" checked";


以避免重建这两个集合(R& ; C)
每个循环


-

Stephane Moriaux et son [moins] vieux Mac


Saul在05/05/2006上午7:01 AEST说:

我有一组动态创建的单选按钮,
rende我通过获取集合的长度尝试循环通过此集合,但是
我一直收到一个错误,指出元素未定义。我正在使用
getElelementsByName因为这些是单选按钮,但似乎没有看到动态元素!!!

这是我的代码...请让我知道如果有什么我做错了! - 谢谢
----
...
...
//通过ajax结构循环---这是正确的所以我知道是
不是Ajax问题。

for(var i = 0; i< ds.Tables [0] .Rows.length; i ++){


is ds .Tables表元素数组?如果是这样,并且item [0]是一个表,

那么它有一个''rows''集合,而不是''Rows''(注意大写)。但是

也许它是你的XML结构的一部分被称为''Rows'',我不知道。


由于你按顺序访问所有行,最好还是一次获得对该行的

引用并从那里开始。


它是更有效地获取一次长度属性并存储它(如果行数很小,那么节省的资金可能是微不足道的,但是,这就是

认为很重要:-)):

var row,rows = ds.Tables [0] .rows;

for(var i = 0,len = rows。 length; i< len; i ++){

row = rows [i];


//创建nue输入元素
var r = document .createElement(''< INPUT type = radio name = company>'');


动态添加元素的方法是IE特定的,并且在大多数(可能是所有)其他浏览器中将失败

,W3C DOM标准方法是:


var r = document.createElement(''input'');

r.type =''radio'';

r.name =''company'';


但是,如果使用该方法,IE在脚本中无法识别名称属性

,虽然表单将正确提交。这里有一个主题




< URL:http://groups.google.co.uk/group/comp.lang。 javascript / browse_frm / thread / db95b9f62035bee6 / a86500f9f5880246?q = name + createElement& rnum = 2#a8650 0f9f5880246>


r.type =" radio" ;;
r。 name =" company";
r.id =" myradiobutton" + i;
r.setAttribute(" value",ds.Tables [0] .Rows [i] .roottype);


setAttribute在某些浏览器中有问题,如上所述直接访问属性更好(尽管

非标准):


r.value = ds.Tables [0] .Rows [i] .roottype;


td_company.appendChild(r);
td_company。 appendChild(document.createTextNode(ds.Tables [0] .Rows [i] .roottypedesc));


使用建议的行&上面的行变量,这变为:


td_company.appendChild(document.createTextNode(row .roottypedesc));


}

-----
我的函数试图获得集合的长度,在渲染
radion按钮后调用。如果我找到一个具有特定值的那个,我会检查它。

for(var n = 0; n< document.getElementsByName(" company")。length; n ++){
if(document.getElementsByName(" company")[n] .value ==''msde'')
{
document.getElementsByName(" country")[n]。 checked =" checked";




同样在这里:


var el,els = document.getElementsByName(" company" ;);

for(var n = 0,len = els.length; n< len; n ++){

el = els [i];

if(el.value ==''msde''){

el.checked = true;

}


-

Rob

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


RobGaécrit:

Saul在05/05/2006上午7:01 AEST说:
[...]

< blockquote class =post_quotes> for(var n = 0; n< document.getElementsByName(" compan) y)。length; n ++){
if(document.getElementsByName(" company")[n] .value ==''msde'')
{
document.getElementsByName( " country")[n] .checked =" checked";



同样在这里:




同样但不是重复(公司) !=国家 )

var el,els = document.getElementsByName(" company");
for(var n = 0,len = els.length; n< len; n ++){
el = els [i];
if(el.value ==''msde''){
el.checked = true;
}




var el,els = document.getElementsByName(" company");

var en,ens = document.getElementsByName(" country");

for(var n = 0,len = els.length; n< len; n ++){

el = els [n]; //而不是els [i]

if(el.value ==''msde''){

en = ens [n];

en.checked = true;

}

}

-

Stephane Moriaux et son [ moins] vieux Mac


I have a set of radio buttons that are created dynamically, after
rendered I try loop thru this set by getting the length of the set, but
I keep getting an error stating the element is undefined. I am using
getElelementsByName since these are radio buttons, but it seems that
the dynamic element is not seen!!!

This is my code... please let me know if there is anything that I am
doing wrong! - thanks
----
....
....
// this loops thru an ajax structure --- this is correct so I kow is
not an Ajax issue.

for(var i=0; i<ds.Tables[0].Rows.length; i++){
//creating nue input element
var r=document.createElement(''<INPUT type=radio name=company >'');
r.type="radio";
r.name ="company";
r.id ="myradiobutton" + i;
r.setAttribute("value",ds.Tables[0].Rows[i].roottype);
td_company.appendChild(r);
td_company.appendChild(document.createTextNode(ds. Tables[0].Rows[i].roottypedesc));
}

-----
My function trying to get the lenght of the set is called after the
radion buttons are rendered. If I find one that has an specific value,
I check it.

for(var n=0; n<document.getElementsByName("company").length; n++){
if (document.getElementsByName("company")[n].value == ''msde'')
{
document.getElementsByName("country")[n].checked = "checked";
}
}

解决方案

Saul a écrit :

I have a set of radio buttons that are created dynamically, after
rendered I try loop thru this set by getting the length of the set, but
I keep getting an error stating the element is undefined. I am using
getElelementsByName since these are radio buttons, but it seems that
the dynamic element is not seen!!!

This is my code... please let me know if there is anything that I am
doing wrong! - thanks
----
...
...
// this loops thru an ajax structure --- this is correct so I kow is
not an Ajax issue.

for(var i=0; i<ds.Tables[0].Rows.length; i++){
//creating nue input element
var r=document.createElement(''<INPUT type=radio name=company >'');
var r=document.createElement(''INPUT'');
r.type="radio";
r.name ="company";
r.id ="myradiobutton" + i;
r.setAttribute("value",ds.Tables[0].Rows[i].roottype);
roottype isn''t it Java language ?
td_company.appendChild(r);
what is td_company ?
td_company.appendChild(document.createTextNode(ds. Tables[0].Rows[i].roottypedesc));
}

-----
My function trying to get the lenght of the set is called after the
radion buttons are rendered. If I find one that has an specific value,
I check it.

for(var n=0; n<document.getElementsByName("company").length; n++){
if (document.getElementsByName("company")[n].value == ''msde'')
{
document.getElementsByName("country")[n].checked = "checked";
}
}



var R = document.getElementsByName("company");
var C = document.getElementsByName("country");
for(var n=0; n<R.length; n++)
if (R[n].value == ''msde'') C[n].checked = "checked";

to avoid to re-built these two collections (R & C)
on each loop
--
Stephane Moriaux et son [moins] vieux Mac


Saul said on 05/05/2006 7:01 AM AEST:

I have a set of radio buttons that are created dynamically, after
rendered I try loop thru this set by getting the length of the set, but
I keep getting an error stating the element is undefined. I am using
getElelementsByName since these are radio buttons, but it seems that
the dynamic element is not seen!!!

This is my code... please let me know if there is anything that I am
doing wrong! - thanks
----
...
...
// this loops thru an ajax structure --- this is correct so I kow is
not an Ajax issue.

for(var i=0; i<ds.Tables[0].Rows.length; i++){
Is ds.Tables an array of table elements? If so, and item[0] is a table,
then it has a ''rows'' collection, not ''Rows'' (note capitalisation). But
maybe it is some part of your XML structure that is called ''Rows'', I
don''t know.

Since you sequentially access all rows, it is also better to get a
reference to the row once and go from there.

It is more efficient to get the length attribute once and store it (the
saving may be insignificant if the number of rows is small, but it''s the
thought that counts :-) ):

var row, rows = ds.Tables[0].rows;
for(var i=0, len=rows.length; i<len; i++){
row = rows[i];

//creating nue input element
var r=document.createElement(''<INPUT type=radio name=company >'');
That method of dynamically adding elements is IE specific and will fail
in most (possibly all) other browsers, the W3C DOM standard method is:

var r = document.createElement(''input'');
r.type = ''radio'';
r.name = ''company'';

However, if that method is used, IE doesn''t recognise the name attribute
in script, though the form will submit correctly. There is a thread
about it here:

<URL:http://groups.google.co.uk/group/comp.lang.javascript/browse_frm/thread/db95b9f62035bee6/a86500f9f5880246?q=name+createElement&rnum=2#a8650 0f9f5880246>

r.type="radio";
r.name ="company";
r.id ="myradiobutton" + i;
r.setAttribute("value",ds.Tables[0].Rows[i].roottype);
setAttribute is buggy in some browsers, it is better (though
non-standard) to access properties directly as you have above:

r.value = ds.Tables[0].Rows[i].roottype;

td_company.appendChild(r);
td_company.appendChild(document.createTextNode(ds. Tables[0].Rows[i].roottypedesc));
Using the suggested row & rows variables above, this becomes:

td_company.appendChild(document.createTextNode(row .roottypedesc));

}

-----
My function trying to get the lenght of the set is called after the
radion buttons are rendered. If I find one that has an specific value,
I check it.

for(var n=0; n<document.getElementsByName("company").length; n++){
if (document.getElementsByName("company")[n].value == ''msde'')
{
document.getElementsByName("country")[n].checked = "checked";



Similarly here:

var el, els = document.getElementsByName("company");
for(var n=0, len=els.length; n<len; n++){
el = els[i];
if (el.value == ''msde'') {
el.checked = true;
}

--
Rob
Group FAQ: <URL:http://www.jibbering.com/FAQ>


RobG a écrit :

Saul said on 05/05/2006 7:01 AM AEST: [...]

for(var n=0; n<document.getElementsByName("company").length; n++){
if (document.getElementsByName("company")[n].value == ''msde'')
{
document.getElementsByName("country")[n].checked = "checked";


Similarly here:



Similarly but not duplicately ( "company" != "country" )
var el, els = document.getElementsByName("company");
for(var n=0, len=els.length; n<len; n++){
el = els[i];
if (el.value == ''msde'') {
el.checked = true;
}



var el, els = document.getElementsByName("company");
var en, ens = document.getElementsByName("country");
for(var n=0, len=els.length; n<len; n++){
el = els[n]; // and not els[i]
if (el.value == ''msde'') {
en = ens[n];
en.checked = true;
}
}
--
Stephane Moriaux et son [moins] vieux Mac


这篇关于getElementsByName未定义动态元素(单选按钮)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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