简体   繁体   中英

Flex Combobox: how to get the value of the selected item?

I am using a combobox for the us states, link . The label is set to the full name of the state, while the value attribute holds the abbreviation. What I want to do is to get the selected item's value. So I tried combo.selectedItem.value and combo.selectedItem.@value, but neither of them worked. Can someone shed a light on this please?

Here's a simple example that might be helpful.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
 <mx:ComboBox id="comboBox" dataProvider="{[{label:'California', value:'CA'}, {label:'New York', value:'NY'}]}" />
 <mx:Label text="{comboBox.selectedItem.value}" />
</mx:Application>


Here's another example. In this one we use XML as dataProvider.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
    <mx:XML id="xml" xmlns="">
        <states>
            <state label="Alabama" value="AL" country="US" />
            <state label="Alaska" value="AK" country="US" />
            <state label="Arkansas" value="AR" country="US" />
        </states>
    </mx:XML>
    <mx:ComboBox id="comboBox" dataProvider="{xml.state}" labelField="@label" />
    <mx:Label text="{comboBox.selectedItem.@value}" />
</mx:Application>

You can populate an array with the values you want to get and retrieve the index of the selected item on the combo box (which should be the same as in the array).

Or in your component ... just look for the index (selected item) child on statesUS

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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