简体   繁体   English

如何从选定的选项中获取文本、名称属性和其他非标准属性?

[英]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中获取任何attributetext

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标签的cityname属性,因为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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM