View Full Version : Can't access selected radio button's value
[Flex3, AS3]
I simply want to get the selected radio button's value when a button is clicked.
In my mind, the following code should work perfectly, but red part returns an error " possibly undefined property" Why? Help please!
<mx:Form id="masterForm">
<mx:FormItem label="Sevice" direction="horizontal" id="serviceForm">
<mx:RadioButton groupName="rb_service" label="Service1" value="1"/>
<mx:RadioButton groupName="rb_service" label="Service2" value="2/>
</mx:FormItem>
<mx:Button label="Send" click="GetValue()" id="sendBtn" tabEnabled="false" buttonMode="true"/>
</mx:Form>
Private Function GetValue():void{
var thevalue:String = rb_service.SelectedValue;
}
Peter Cowling
09-10-2009, 11:20 PM
Add the property 'id' to your radio buttons.
Give them unique names.
Reference those names in your method.
A GroupName reference as per now will not work.
wvxvw
09-11-2009, 12:01 AM
Also Private Function GetValue():void{
Should really be:
private function getValue():void{
No matter what it does.
And, you should really look into manual when trying to access properties you imagine the control may have, there for certain cannot be properties in Flex that start with uppercase letter.
One more thing: almost every component may dispatch events - this is a way for you to track down the changes in the component. Again, you can find the list of the events under the list of the methods for that component in ASDocs. Besides being helpful in recording component change events have a special ability to always tell you which component had dispatched them (through event.target or event.currentTarget).
By knowing this you could then tell which radiobutton had changed it's state.
So, for example, RadioButtonGrup may dispatch itemClick (http://livedocs.adobe.com/flex/3/langref/mx/controls/RadioButtonGroup.html#event:itemClick) event, this event has item property to tell you what dataProvider element is associated with the clicked radiobutton and relatedObject property to tell you which radiobutton was clicked.
thank you. But I am still having problems.
I do not want to use item click event with radio button control because there are other form items and I want to collect them all when the button is clicked. I added the ids. How do I access the selected value?
<mx:Form id="masterForm">
<mx:FormItem label="Sevice" direction="horizontal" id="serviceForm">
<mx:RadioButton id="r1" groupName="rb_service" label="Service1" value="1"/>
<mx:RadioButton id="r2" groupName="rb_service" label="Service2" value="2/>
</mx:FormItem>
<mx:Button label="Send" click="getValue()" id="sendBtn" tabEnabled="false" buttonMode="true"/>
</mx:Form>
Private Function getValue():void{
// This doesn't work.
var thevalue:String = rb_service.SelectedValue;
// This also doesn't work.
if (r1.selected == true){ blah blah ;}
// This also doesn't work.
if (rb.r1.selected == true){ blah blah ;}
}
Peter Cowling
09-12-2009, 09:42 AM
Hi,
conditional evaluation of Boolean's should use absolute equivalency i.e. == should be ===.
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.