-
[as3] combobox data?
How do I get the data from a combobox?
-
Senior Member
Here is an example if you want the label:
PHP Code:
mc.addItem({label:"item1"});
mc.addItem({label:"item2"});
mc.addItem({label:"item3"});
mc.addEventListener(Event.CHANGE, changeHandler);
function changeHandler(ev:Event):void
{
trace(ev.currentTarget.selectedItem.label);
}
- The right of the People to create Flash movies shall not be infringed. -
-
I don't need the label, I need the data
-
5+5=55
Then use: trace(ev.currentTarget.selectedItem.data);
-
It works! Thankyou, thankyou, thankyou!!!
I could hug you. But you're impossible far away from me for a hug
Thanks
-
... How do I convert the contents of a textfield as an int?
-
got it
var number:Number = new Number(txt.text);
-
Senior Member
The "label" is also data.
- The right of the People to create Flash movies shall not be infringed. -
-
I know this is an extremely old thread, but I am trying to do something similar and did not want to start a whole new thread about the same subject, I am trying to get the total amounts from the 4 comboBoxes, into a single text field.
Here is my code so far:
Actionscript Code:
import fl.controls.ComboBox; import flash.text.TextField; import flash.text.TextFieldType;
myComboBox.addEventListener(Event.CHANGE, clickHandler); myComboBox.addEventListener(Event.CHANGE, totalamount); var tf:TextField = new TextField(); addChild(tf); tf1.alwaysShowSelection = true;
function clickHandler(event:Event):void { tf1.text = ComboBox(event.target).selectedItem.data; }
myComboBox2.addEventListener(Event.CHANGE, clickHandler2); myComboBox2.addEventListener(Event.CHANGE, totalamount); tf2.alwaysShowSelection = true;
function clickHandler2(event:Event):void { tf2.text = ComboBox(event.target).selectedItem.data; }
myComboBox3.addEventListener(Event.CHANGE, clickHandler3); myComboBox3.addEventListener(Event.CHANGE, totalamount); tf3.alwaysShowSelection = true;
function clickHandler3(event:Event):void { tf3.text = ComboBox(event.target).selectedItem.data; }
myComboBox4.addEventListener(Event.CHANGE, clickHandler4); myComboBox4.addEventListener(Event.CHANGE, totalamount); tf4.alwaysShowSelection = true;
function clickHandler4(event:Event):void { tf4.text = ComboBox(event.target).selectedItem.data; }
function totalamount(event:Event):void { total.text = tf1.text + tf2.text + tf3.text + tf4.text; total.alwaysShowSelection = true; }
Now, what I have works to get the data, but as you can see, it just adds the text fields and doesn't really do any math to calculate a total amount. I am just learning AS3, from AS2, and I've having a bugger of a time getting it, AS2 was hard enough for me. I'm a designer, not a programmer, but I gotta learn this stuff. Any help is greatly appreciated! Thanks!
-
Senior Member
when you get the .text of a textfield, you're getting a string. Using + between strings concatenates them, it doesn't add them as numbers. You need to cast them to Number or int.
Change the second to last line of your code to
Actionscript Code:
total.text = String(Number(tf1.text) + Number(tf2.text) + Number(tf3.text) + Number(tf4.text));
-
Thank you for the response, I changed the code, but I get "NaN" in my totals text field now
EDIT**
I got the NAN to go away, basically it was because I had a $ sign in my data field in the ComboBox. But I have another question, if you don't mind- when my totals are added up, i sometimes get 4 or 5 digits behind the decimal (e.g. 35.49999), how can i limit the total to just be, 35.49?
Thanks!!
Last edited by nutless_neo; 01-03-2012 at 06:54 PM.
-
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|