Recipe 11.9 Getting the Selected Radio Button Value

11.9.1 Problem

You want to get the value of the selected radio button from a group.

11.9.2 Solution

Use the getValue( ) method.

11.9.3 Discussion

First, create two radio buttons and assign them to the same group (named "answerGroup"):

_root.attachMovie("FRadioButtonSymbol", "answer0_rb", 1); _root.attachMovie("FRadioButtonSymbol", "answer1_rb", 2); answer0_rb.setGroupName("answerGroup"); answer1_rb.setGroupName("answerGroup"); answer0_rb.setLabel("Hot"); answer0_rb.setData (212); answer1_rb.setLabel("Cold"); answer1_rb.setData (32); answerGroup.setPositions(  );

Some time later, you can obtain the data value of the selected radio button by invoking the getValue( ) method on the radio button group. The radio button group is created automatically when a radio button is added to a group using setGroupName( ). In this example, the return value is either 212 or 32, not the string "Hot" or "Cold". That is, getValue( ) returns the selected button's data property, not its label property.

selectedVal = answerGroup.getValue(  );

As with getting the selected values for other UI components, you typically get the values for radio button groups once the user clicks on the Submit button:

function onClick (  ) {   var selectedVal = answerGroup.getValue(  );   // Do something with data here. } submitBtn.setClickHandler("onClick");

11.9.4 See Also

Recipe 11.6, Recipe 11.7, and Recipe 11.11



ActionScript Cookbook
ActionScript 3.0 Cookbook: Solutions for Flash Platform and Flex Application Developers
ISBN: 0596526954
EAN: 2147483647
Year: 2005
Pages: 425

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net