最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - YUI 3: Getting the selected option's text - Stack Overflow

programmeradmin0浏览0评论

I am working with YUI 3, ing from jQuery, but I have a question about YUI usage.

I have a select tag with some option tags:

 <select id="ownerSelector">
    <option></option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
 </select>

I want to get the selected option's text.

Is there anything resembling jQuery's $('option:selected') extension in YUI 3?

I saw this over at that a way to do this is Y.all('option[selected]'), but that doesn't work. (Either that, or I don't know which YUI module the [] selector syntax belongs to.)

If there is no shortcut, I noticed that when I query Y.all('#ownerSelector option'), I can see the NodeList array, and one of the options has a property called 'selected'. Is there a way to get at the selected option?

I am working with YUI 3, ing from jQuery, but I have a question about YUI usage.

I have a select tag with some option tags:

 <select id="ownerSelector">
    <option></option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
 </select>

I want to get the selected option's text.

Is there anything resembling jQuery's $('option:selected') extension in YUI 3?

I saw this over at http://www.jsrosettastone./#selectors that a way to do this is Y.all('option[selected]'), but that doesn't work. (Either that, or I don't know which YUI module the [] selector syntax belongs to.)

If there is no shortcut, I noticed that when I query Y.all('#ownerSelector option'), I can see the NodeList array, and one of the options has a property called 'selected'. Is there a way to get at the selected option?

Share Improve this question edited Aug 3, 2013 at 11:27 Stephen asked Aug 2, 2013 at 19:47 StephenStephen 2,5703 gold badges34 silver badges60 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

I think this is what you're looking for:

YUI().use("selector-css3", "node", function (Y) {
    var text = Y.one("#ownerSelector option:checked").get("text");
});

http://jsfiddle/aqPus/2/

It doesn't look like YUI 3 supports selector shortcuts. This is what I could find on the YUI forums.

 var node = Y.one('#ownerSelector');
 node.get('options').item(node.get('selectedIndex')).get('text');
Y.one('#ownerSelector').get('value')
发布评论

评论列表(0)

  1. 暂无评论