查询控件数组 [英] Query on arrays of controls

查看:48
本文介绍了查询控件数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



有充分证明,如果在页面上有几个具有相同名称的radio-

按钮,则这些按钮将作为数组进行处理(并且行为

统称)。


有人(LRN?)最近写过另一个案例,其中名称匹配

意味着数组寻址。


因此我尝试使用以下原始网页:


< head>< / head>< body>


< form name = F1>

< input type = text name = A size = 3 value = 00>

< input type = text name = A size = 3 value = 11>

< input type = text name = A size = 3 value = 22>

< input type =按钮名称= B onClick = X()value =" ?? ">

< / form>


< script>

J = 0


函数X(){

F1.B.value =" " + F1.A [J ++%3] .value +"

}


< / script>

< / body>


在它上面,在我的MSIE4中,按下?按钮重复循环它的

图例到00 11 22 00 11 22 00 ...这表明这样的

a组名称匹配控件可以在那里作为数组访问。


合法且一般可用吗?我不记得读了

吧。


特别是,我的桌子原则上类似于


2001 2002 2003 2004 2005

St P星期六星期一星期一星期三星期四

星期一星期六星期六太阳星期二周三


并希望将列自动更改为Year-2..Year + 2 for any

(当前)年份,我可以用St P行五个实例

< input type = text name = StP size = 5 readonly>并依次计算

元素[0] .. [4]这个数组(同样适用于其他行),并且

它成功地用于所有合理的启用javascript的

浏览器?


我可以使用< div> ..< / div>因此,使用DynWrite,而不是< input

....>?


我推迟了动态列数的可能性如此

一周中每天至少有一个实例

在2月28日的行和3月1日的行中。


-

?约翰斯托克顿,英国萨里。 ?@merlyn.demon.co.uk Turnpike v4.00 IE 4?

< URL:http://jibbering.com/faq/> Jim Ley的新闻常见问题解答:comp.lang.javascript

< URL:http://www.merlyn.demon.co.uk/js-index.htm> JS数学,日期,来源。

< URL:http://www.merlyn.demon.co.uk/> TP / BP / Delphi / JS /& c。,FAQ主题,链接。


It is well-documented that if on a page there are several radio-
buttons with the same name these are addressed as an array (and act
collectively).

Someone (LRN?) recently wrote about another case where name-match
implies array addressing.

It therefore occurred to me to try the following crude Web page :

<head></head><body>

<form name=F1>
<input type=text name=A size=3 value=00>
<input type=text name=A size=3 value=11>
<input type=text name=A size=3 value=22>
<input type=button name=B onClick=X() value=" ?? ">
</form>

<script>
J=0

function X() {
F1.B.value = " " + F1.A[J++%3].value + " "
}

</script>
</body>

On it, in my MSIE4, pressing the ?? button repeatedly cycles its
legend through 00 11 22 00 11 22 00 ... which demonstrates that such
a set of name-matched controls can be accessed as an array there.

Is that legitimate and generally available? I do not recall reading
it.

In particular, where I have a table similar in principle to

2001 2002 2003 2004 2005
St P Sat Sun Mon Wed Thu
St G Mon Tue Wed Fri Sat
St A Fri Sat Sun Tue Wed

and wish to auto-change the columns to be Year-2..Year+2 for any
(current) Year, can I use for the St P row five instances of
<input type=text name=StP size=5 readonly> and compute in turn
elements [0]..[4] of this array (likewise for other rows), and have
it working successfully for all reasonable javascript-enabled
browsers?

Could I use <div>..</div> with DynWrite thus, instead of <input
....>?

I postpone the possibility of having a dynamic column-count such
that there is at least one instance of each of the days of the week
in both the row for Feb 28 and that for Mar 1.

--
? John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ?
<URL:http://jibbering.com/faq/> Jim Ley''s FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

推荐答案

John Stockton博士于2003年11月27日写道:


< snip>
Dr John Stockton wrote on 27 Nov 2003:

<snip>
<输入类型=按钮名称= B onClick = X()value =" ?? ">


省略onclick属性值周围的报价可能有意为
,但我想指出唯一的非

可能出现在未加引号的属性中的字母数字字符

值是连字符,句号,下划线和冒号。


< snip>

在它上面,在我的MSIE4中,按下?按钮反复循环其传说通过00 11 22 00 11 22 00 ...这表明
这样一组名称匹配的控件可以在那里作为数组访问。

这是合法的,一般可用吗?我不记得读它了。


Netscape的JavaScript参考(v1.3)在

的介绍中说明了一组同名的单选按钮的Radio对象br />
可以使用下标编入索引。此外,DOM方法

getElementsByName将返回给出其名称值的

元素的(可能为空)集合。在方法的论证中。

特别是,我有一张原则上类似的表格

2001 2002 2003 2004 2005
St P Sat Sun Mon Wed星期四星期二星期五星期五星期五星期五星期三星期三星期三星期三星期三星期三星期三

并希望自动将列更改为年级2年。年份+ 2表示任何
(当前)年份,我可以使用St P行的五个实例
< input type = text name = StP size = 5 readonly>并依次计算
元素[0] .. [4]这个数组(同样适用于其他行),并且它已经成功地用于所有合理的启用javascript的浏览器?


只有类型为radio和checkbox的INPUT可以共享控件名称。一个

解决方案是使用一个数组,其中包含每个

文本框的名称,索引,并使用元素属性为

a表单对象,或者,如果它们是id值,则作为

getElementById方法的参数。

我可以使用< div> ..< / div> ;因此,使用DynWrite而不是< input
...>?
<input type=button name=B onClick=X() value=" ?? ">
Omission of the quotes around the onclick attribute value may have
been intentional, but I would like to point out that the only non-
alphanumeric characters that may appear in an unquoted attribute
value are hyphens, periods, underscores and colons.

<snip>
On it, in my MSIE4, pressing the ?? button repeatedly cycles its
legend through 00 11 22 00 11 22 00 ... which demonstrates that
such a set of name-matched controls can be accessed as an array
there.

Is that legitimate and generally available? I do not recall
reading it.
Netscape''s JavaScript reference (v1.3) states in the introduction for
the Radio object that a group of radio buttons under the same name
may be indexed with subscripts. In addition, the DOM method,
getElementsByName, will return "the (possibly empty) collection of
elements whose name value is given" in the argument to the method.
In particular, where I have a table similar in principle to

2001 2002 2003 2004 2005
St P Sat Sun Mon Wed Thu
St G Mon Tue Wed Fri Sat
St A Fri Sat Sun Tue Wed

and wish to auto-change the columns to be Year-2..Year+2 for any
(current) Year, can I use for the St P row five instances of
<input type=text name=StP size=5 readonly> and compute in turn
elements [0]..[4] of this array (likewise for other rows), and
have it working successfully for all reasonable
javascript-enabled browsers?
Only INPUTs of type radio and checkbox can share control names. A
solution would be to use an array that contained the names of each
text box, index that, and use the value with the elements property of
a Form object or, if they are id values, as the argument to the
getElementById method.
Could I use <div>..</div> with DynWrite thus, instead of <input
...>?




如果您指的是动态修改已加载的页面,

我不知道。如果没有,你能解释一下你的意思吗?


迈克


-

Michael Winter
M.******@blueyonder.co.uk.inva 盖子(删除.invalid以回复)



If you are referring to dynamically altering an already loaded page,
I wouldn''t know. If not, could you explain what you mean here?

Mike

--
Michael Winter
M.******@blueyonder.co.uk.invalid (remove ".invalid" to reply)


John Stockton博士< sp ** @ merlyn.demon.co.uk>写道:
Dr John Stockton <sp**@merlyn.demon.co.uk> writes:
有充分证据表明,如果在页面上有几个名称相同的单选按钮,则这些按钮将作为数组进行寻址(并执行
集体)。
有人(LRN?)最近写了另一个案例,其中名称匹配
意味着数组寻址。


可能。它适用于所有表单控件。尝试:


< form id =" foo">

< input type =" text"名称=英寸×" value =" 42">

< input type =" radio"名称=英寸×" value =" 37">

< input type ="复选框"名称=英寸×" value =" 87">

< select name =" x">< option value =" 13" selected =" selected"> X< / option>< / select>

< / form>

< script type =" text / javascript" >

var xs = document.forms [''foo'']。elements [''x''];

alert([xs.length,xs [ 0] .value,xs [1] .value,xs [2] .value,xs [3] .value]);

< / script>


在IE6,Opera 7和Mozilla中提醒

4,42,47,87,13

(在Netsape 4中还有一些改写)。


J = 0
函数X(){
F1.B.value =" " + F1.A [J ++%3] .value +"
}
在它上面,在我的MSIE4中,按下??按钮反复循环其传说通过00 11 22 00 11 22 00 ...这表明这样一组名称匹配的控件可以作为数组访问。


这不是阵列。如果您使用... instanseof Array进行检查,则会给出

false。在Opera中,它是一个对象。在Mozilla中,它是一个NodeList。

重要的是它没有动态长度或者Array.prototype的方法



这是合法的吗?一般可用?


除了写作

FBvalue

而不是

document.forms.F.elements .B.value

然后是。 (使用F作为全局变量在Mozilla中不起作用)

我不记得读它了。


我在DOM规范中找不到它。

HTMLFormDocument'的'元素'属性是HTMLCollection。

它的namedItem方法只允许返回一个节点。


这可能是因为DOM规范被写入

与类型语言兼容,所以" namedItem"方法

在某些情况下不能返回Node,而在其他情况下不能返回NodeList(NodeList

是返回的,例如getElementsByTagName)


所以,你必须查看浏览器特定的DOM才能找到任何东西。


在微软的文档中,你会发现:

---

如果此参数是一个字符串并且有多个元素

且name或id属性等于字符串,则该方法返回

a匹配元素的集合。

---

Netscape文档中有类似的东西。

特别是,我的桌子原则上与
2001 2002 2003 2004 2005相似
St P星期六星期一星期三星期三星期四星期二星期三星期五星期五星期六星期五星期五星期五星期六星期六星期三

并希望自动将列更改为Year-2..Year + 2 for a ny
(当前)年份,我可以用于St P行的五个实例
< input type = text name = StP size = 5 readonly>并依次计算
元素[0] .. [4]这个数组(同样适用于其他行),并且它已成功地适用于所有合理的启用javascript的浏览器?


应该有效。但是如果你有一个已知宽度/高度的桌子,你可以根据坐标命名元素,例如

name =" St(2,3 )

我可以使用< div> ..< / div>因此,使用DynWrite而不是< input
...>?
It is well-documented that if on a page there are several radio-
buttons with the same name these are addressed as an array (and act
collectively). Someone (LRN?) recently wrote about another case where name-match
implies array addressing.
Probably. It works that way for all form controls. Try:

<form id="foo">
<input type="text" name="x" value="42">
<input type="radio" name="x" value="37">
<input type="checkbox" name="x" value="87">
<select name="x"><option value="13" selected="selected">X</option></select>
</form>
<script type="text/javascript">
var xs = document.forms[''foo''].elements[''x''];
alert([xs.length,xs[0].value,xs[1].value,xs[2].value,xs[3].value]);
</script>

It alerts
4,42,47,87,13
in IE6, Opera 7, and Mozilla (and with some rewriting also in Netsape 4).

J=0

function X() {
F1.B.value = " " + F1.A[J++%3].value + " "
} On it, in my MSIE4, pressing the ?? button repeatedly cycles its
legend through 00 11 22 00 11 22 00 ... which demonstrates that such
a set of name-matched controls can be accessed as an array there.
It''s not an array. If you check with "... instanseof Array", it gives
false. In Opera, it''s an Object. In Mozilla, it''s a NodeList. The
important thing is that it doesn''t have a dynamic length or the methods
of Array.prototype.
Is that legitimate and generally available?
Apart from writing
F.B.value
instead of
document.forms.F.elements.B.value
then yes. (It wouldn''t work in Mozilla using F as a global variable)
I do not recall reading it.
I can''t find it in the DOM specification.
The HTMLFormDocument''s "elements" property is a HTMLCollection.
Its "namedItem" method is only allowed to return a single Node.

It is probably a result of the DOM specification being written
to be compatible with typed languages, so the "namedItem" method
can''t return a Node in some cases and a NodeList in others (NodeList
is what is returned by, e.g., getElementsByTagName)

So, you''ll have to look at the browser specific DOMs to find anything.

in Microsoft''s documentation, you find:
---
If this parameter is a string and there is more than one element
with the name or id property equal to the string, the method returns
a collection of matching elements.
---
There is problably something similar in a Netscape documentation.
In particular, where I have a table similar in principle to 2001 2002 2003 2004 2005
St P Sat Sun Mon Wed Thu
St G Mon Tue Wed Fri Sat
St A Fri Sat Sun Tue Wed

and wish to auto-change the columns to be Year-2..Year+2 for any
(current) Year, can I use for the St P row five instances of
<input type=text name=StP size=5 readonly> and compute in turn
elements [0]..[4] of this array (likewise for other rows), and have
it working successfully for all reasonable javascript-enabled
browsers?
It should work. But if you have a table with a known width/height, you
can just name the elements based on the coordinates, e.g.
name="St(2,3)"
Could I use <div>..</div> with DynWrite thus, instead of <input
...>?




一般情况下,不是。没有名称 div上的属性,以及

" id"属性必须是唯一的。在IE中,你可以有几个元素

具有相同的id,而document.all / document.getElementById将

返回一个集合。这不便携。


/ L

-

Lasse Reichstein Nielsen - lr*@hotpop.com

DHTML死亡颜色:< URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>

''没有判断的信仰只会降低神灵的精神。''



Not generally, no. There is no "name" attribute on a div, and the
"id" attribute must be unique. In IE, you can have several elements
with the same id, and document.all/document.getElementById will
return a collection. That is not portable.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
''Faith without judgement merely degrades the spirit divine.''


Michael Winter< M。****** @ blueyonder.co .uk.invalid>写道:
Michael Winter <M.******@blueyonder.co.uk.invalid> writes:
只有类型为radio和checkbox的INPUT可以共享控件名称。
Only INPUTs of type radio and checkbox can share control names.




参考?我在HTML 4.01规范中看不到这样的限制。


确实*提示*它:

---

几个表单中的复选框可以共享相同的控件名称。

---



---

单选按钮就像复选框,除了当几个共享

相同的控件名称时,它们是互斥的

---

没有其他控件有它的名字提到了。

或许这是他们的意思

---

FORM中控件的name属性的范围元素

是FORM元素。

---


/ L

-

Lasse Reichstein Nielsen - lr*@hotpop.com

DHTML死亡颜色:<网址:http://www.infimum.dk/HTML/rasterTriangleDOM.html>

''没有判断的信仰只会降低精神神圣。''



Reference? I see no such restriction in the HTML 4.01 specification.

It does *hint* it:
---
Several checkboxes in a form may share the same control name.
---
and
---
Radio buttons are like checkboxes except that when several share the
same control name, they are mutually exclusive
---
No other control has its name mentioned.
perhaps it is what they mean by
---
The scope of the name attribute for a control within a FORM element
is the FORM element.
---

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
''Faith without judgement merely degrades the spirit divine.''

这篇关于查询控件数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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