[英]How to get text, name attribute and other non-standard attributes from selected option?
I have a select
html-tag with several options
.我有一个
select
html-tag 有几个options
。
Here is an example:这是一个例子:
<select id="mySelect1" size="4" style="width:100px;">
<option value="AUS"
name="Australia"
city="Canberra"
>Australia</option>
<option selected value="CAN"
name="North America"
city="Ottawa"
>Canada</option>
<option value="GRC"
name="Europe"
city="Athens"
>Greece</option>
<option value="JPN"
name="Asia"
city="Tokyo"
>Japan</option>
</select>
I know how to alert
the value
of the selected option:我知道如何
alert
所选选项的value
:
alert(mySelect1.options[mySelect1.options.selectedIndex].value);
But when I try to alert
the city
or even the name
of the selected option I fail, with almost the same code:但是,当我尝试
alert
city
甚至所选选项的name
时,我失败了,代码几乎相同:
alert(mySelect1.options[mySelect1.options.selectedIndex].city);
alert(mySelect1.options[mySelect1.options.selectedIndex].name);
Is it possible to get any attribute
and the text
from the selected option
in pure javascript?是否可以从纯 javascript 中的选定
option
中获取任何attribute
和text
?
Sure, it is possible.当然,这是可能的。
First of all, your code does not work with city
and name
attributes of the option
tag because HTML option Tag has neither name
, nor city
attribute , they are not standard!首先,您的代码不适用于
option
标签的city
和name
属性,因为HTML option Tag既没有name
也没有city
属性,它们不是标准的!
If you ever wish, you can use a standard label
attribute instead of name
, so the similar minimalist code would work:如果您愿意,您可以使用标准的
label
属性而不是name
,因此类似的极简代码可以工作:
//Get label attribute:
alert(mySelect1.options[mySelect1.options.selectedIndex].label);
But in any way, apart from name
, you've got city
.但无论如何,除了
name
,你还有city
。 So you need to use attributes
for getting such non-standard attributes.因此,您需要使用
attributes
来获取此类非标准属性。 Therefore, here is the code that works with the selected option, based on your example:因此,根据您的示例,这是适用于所选选项的代码:
document.write('<br>'); //Get innerText: document.write(mySelect1.options[mySelect1.options.selectedIndex].innerText + '<br>'); //Get value attribute: document.write(mySelect1.options[mySelect1.options.selectedIndex].value + '<br>'); //Get name attribute: document.write(mySelect1.options[mySelect1.options.selectedIndex].attributes.name.value + '<br>'); //Get city attribute: document.write(mySelect1.options[mySelect1.options.selectedIndex].attributes.city.value + '<br>');
<select id="mySelect1" size="4" style="width:100px;"> <option value="AUS" name="Australia" city="Canberra" >Australia</option> <option selected value="CAN" name="North America" city="Ottawa" >Canada</option> <option value="GRC" name="Europe" city="Athens" >Greece</option> <option value="JPN" name="Asia" city="Tokyo" >Japan</option> </select>
So this is how it works.这就是它的工作原理。
For you to understand more, all attributes can be retrieved from attributes
.为了让您了解更多,所有属性都可以从
attributes
中检索。 That means that you can (but of course do not need to! ) use attributes
with standard attributes, like this:这意味着您可以(但当然不需要! )将
attributes
与标准属性一起使用,如下所示:
alert(mySelect1.options[mySelect1.options.selectedIndex].attributes.value.value);
alert(mySelect1.options[mySelect1.options.selectedIndex].attributes.label.value);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.