具有相同名称的多个选择元素 [英] Multiple select elements with same name

查看:123
本文介绍了具有相同名称的多个选择元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


表格中有一个或多个具有相同名称的选择元素。

让我们说出的名称是select是''select1'',

表单的名称是''form1''。

如果只有一个选择我像form1一样访问它。 select1。

如果有多个选择,我会像

form1.select1 [index]一样访问它们。


To检查是否有多个,我想我可以使用

if(form1.select1.length){

....

}

其他{

....

}


但这不是工作,因为如果只有一个选择,

form1.select1.length是一个有效的属性,其长度为

选项。

你通常如何处理这个问题?


提前致谢。


Sam

Hello,

There are 1 or more select elements with the same name in a form.
Let''s say that the name of the select is ''select1'' and the name of the
form is ''form1''.
If there''s only one select I access it like form1.select1.
If there are more than one select, I access them like
form1.select1[index].

To check if there are more than one, I thought I can use
if (form1.select1.length) {
....
}
else {
....
}

But that doesn''t work because if there''s only one select,
form1.select1.length is a valid property which is the length of
options.

How do you usually handle this problem?

Thanks in advance.

Sam

推荐答案

Sam Kong说:
Sam Kong said:
>
>
你好,

表格中有一个或多个具有相同名称的选择元素。
让我们说这个名字select是''select1'',
表单的名称是''form1''。
如果只有一个选择我访问它就像form1.select1。
如果有多个选择,我访问它们就像
form1.select1 [index]。

要检查是否有多个,我想我可以使用
如果(form1.select1.length){
...
}
其他{
...
}

但那不是'不行,因为如果只有一个选择,
form1.select1.length是一个有效的属性,它是
选项的长度。

你通常如何处理这个问题?
>
Hello,

There are 1 or more select elements with the same name in a form.
Let''s say that the name of the select is ''select1'' and the name of the
form is ''form1''.
If there''s only one select I access it like form1.select1.
If there are more than one select, I access them like
form1.select1[index].

To check if there are more than one, I thought I can use
if (form1.select1.length) {
...
}
else {
...
}

But that doesn''t work because if there''s only one select,
form1.select1.length is a valid property which is the length of
options.

How do you usually handle this problem?



我认为通常要做的就是换一个名字。

-

I think the usual thing to do would be to change one of the names.
--


5月11日下午7:31,Sam Kong< sam.sk .. @ gmail.comwrote:
On May 11, 7:31 pm, Sam Kong <sam.s.k...@gmail.comwrote:

您好,


表格中有一个或多个具有相同名称的选择元素。

让我们说选择的名称是' 'select1''和

表单的名称是''form1''。

如果只有一个选择我访问它就像form1.select1。

如果有多个选择,我会像

form1.select1 [index]一样访问它们。


检查是否存在不止一个,我想我可以用

if(form1.select1.length){

...}


其他{

...


}


但这不起作用,因为如果有的话只有一个选择,

form1.select1.length是一个有效的属性,它是len g /

选项。


你通常如何处理这个问题?


提前谢谢。


Sam
Hello,

There are 1 or more select elements with the same name in a form.
Let''s say that the name of the select is ''select1'' and the name of the
form is ''form1''.
If there''s only one select I access it like form1.select1.
If there are more than one select, I access them like
form1.select1[index].

To check if there are more than one, I thought I can use
if (form1.select1.length) {
...}

else {
...

}

But that doesn''t work because if there''s only one select,
form1.select1.length is a valid property which is the length of
options.

How do you usually handle this problem?

Thanks in advance.

Sam



var elements = document.getElementsByName(" select1");


这将返回一个具有指定名称的元素数组。


如果需要,您可以迭代它,并对每个元素执行操作,

或只需使用长度即可。看看有多少。


alert(elements.length);


Lee提出的解决方案并不错。虽然名字不像

ids(从某种意义上说它们不一定是独一无二的),但我没有看到任何

点数超过一个具有相同名称的元素。你会发现自己很可能会遇到很多问题。


我觉得提供任何需要识别的东西都是很好的做法
名称和ID。 id和名称应匹配,从而为您需要的所有内容提供

唯一名称。显然,如果它是一个不需要id的东西,但只是一个名字,反之亦然,

然后不给它一。但是如果你给一个元素一个id为

" select1",即使你不给它一个名字,我也会考虑

" select1"不使用任何其他元素名称。你会发现

这样的编码可以帮助你理解并节省很多时间

修复问题的时间。


全部最好的。


Daz。

var elements = document.getElementsByName("select1");

This returns an array of elements with the specified name.

You can then iterate through it if you want, and do stuff with each,
or just use "length" to see how many there are.

alert(elements.length);

Lee''s proposed solution is not a bad one. Whilst names are not like
ids (in the sense that they don''t have to be unique), I don''t see any
point in having more than one element with the same name. You will
most likely find yourself running into a lot of problems.

I find it good practice to give anything that needs to be identified a
name and an id. The id and the name should match, thus giving you a
unique name for everything that needs one. Obviously, if it''s
something that doesn''t need an id, but only a name, or vice-versa,
then don''t give it one. But if you give one element an id of
"select1", even if you don''t give it a name, I would consider
"select1" to be out of use for any other element names. You will find
that coding like this helps you understand things and saves you a lot
of time fixing problems.

All the best.

Daz.


5月11日下午12:20,Daz< cutenfu。 .. @ gmail.comwrote:
On May 11, 12:20 pm, Daz <cutenfu...@gmail.comwrote:

5月11日晚上7:31,Sam Kong< sam.sk .. @ gmail.comwrote:
On May 11, 7:31 pm, Sam Kong <sam.s.k...@gmail.comwrote:

Hello,
Hello,


表格中有1个或多个具有相同名称的选择元素。 br />
假设选择的名称是''select1'',而

表格的名称是''form1''。

如果只有一个选择我访问它就像form1.select1。

如果有多个选择,我访问它们就像

form1.select1 [指数]。
There are 1 or more select elements with the same name in a form.
Let''s say that the name of the select is ''select1'' and the name of the
form is ''form1''.
If there''s only one select I access it like form1.select1.
If there are more than one select, I access them like
form1.select1[index].


要检查是否有多个,我想我可以使用

if(form1.select1.length) {

...}
To check if there are more than one, I thought I can use
if (form1.select1.length) {
...}


else {

...
else {
...


}
}


但这不起作用,因为如果有的话只有一个选择,

form1.select1.length是一个有效的属性,它是

选项的长度。
But that doesn''t work because if there''s only one select,
form1.select1.length is a valid property which is the length of
options.


你通常如何处理这个问题?
How do you usually handle this problem?


先谢谢。
Thanks in advance.


Sam
Sam



var elements = document.getElementsByName(" select1");


这将返回一个具有指定名称的元素数组。


如果需要,您可以迭代它,并对每个元素执行操作,

或只是使用长度看看有多少。


alert(elements.length);


Lee提出的解决方案并不错。虽然名字不像

ids(从某种意义上说它们不一定是独一无二的),但我没有看到任何

点数超过一个具有相同名称的元素。你会发现自己很可能会遇到很多问题。


我觉得提供任何需要识别的东西都是很好的做法
名称和ID。 id和名称应匹配,从而为您需要的所有内容提供

唯一名称。显然,如果它是一个不需要id的东西,但只是一个名字,反之亦然,

然后不给它一。但是如果你给一个元素一个id为

" select1",即使你不给它一个名字,我也会考虑

" select1"不使用任何其他元素名称。你会发现

这样的编码可以帮助你理解并节省很多时间

修复问题的时间。


全部最好的。


Daz。


var elements = document.getElementsByName("select1");

This returns an array of elements with the specified name.

You can then iterate through it if you want, and do stuff with each,
or just use "length" to see how many there are.

alert(elements.length);

Lee''s proposed solution is not a bad one. Whilst names are not like
ids (in the sense that they don''t have to be unique), I don''t see any
point in having more than one element with the same name. You will
most likely find yourself running into a lot of problems.

I find it good practice to give anything that needs to be identified a
name and an id. The id and the name should match, thus giving you a
unique name for everything that needs one. Obviously, if it''s
something that doesn''t need an id, but only a name, or vice-versa,
then don''t give it one. But if you give one element an id of
"select1", even if you don''t give it a name, I would consider
"select1" to be out of use for any other element names. You will find
that coding like this helps you understand things and saves you a lot
of time fixing problems.

All the best.

Daz.



Daz,


谢谢你的回答。

我想你提供了正确的解决方案和良好的建议。


Sam

Daz,

Thank you for the kind answer.
I think you provided the right solution and good advice.

Sam


这篇关于具有相同名称的多个选择元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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