11.11. Getting Checkbox Values
Problem
You want to determine the
values
(true or false) of checkboxes.
Solution
Use the getValue( ) method
for each checkbox, or create a custom
getValues( ) method for the checkbox group.
Discussion
The getValue( ) method returns
true if a checkbox is checked and
false if the checkbox is unchecked:
trace(myCheckBox0_ch.getValue( ));
If you have created a checkbox group (see Recipe 11.10), you can add a custom method,
getValues( ), that returns an array of the
values for each checkbox in the group. The elements of the returned
array contain three properties:
-
name The name of the checkbox instance
-
label The label value of the checkbox
-
value The value of the checkbox (
trueorfalse)
Here is our custom getValues( ) method, which
you should add to your Form.as file for easy
inclusion in future projects:
// Create the new method,getValues( ), for theFCheckBoxGroupClass. FCheckBoxGroupClass.prototype.getValues = function ( ) { // ThedataArarray is populated with the objects for each checkbox in the group. var dataAr = new Array( ); // Create two local variables for use in theforstatement. var cb, obj; // Loop through every checkbox in the group. for (var i = 0; i < this.radioInstances.length; i++) { //cbrefers to the current checkbox. cb = this.radioInstances[i]; // For each checkbox, create an object withname,label, andvalueproperties. obj = new Object( ); obj.name = cb._name; obj.label = cb.getLabel( ); obj.value = cb.getValue( ); // Add the object ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access