Checkbox as3 function

为君一笑 提交于 2019-12-13 06:19:46

问题


I have 16 checkboxes and I need to see which of them was selected. Is there a function that can do that ? I am able to do them with if() but it will be way to long thanks for the help !!


回答1:


Put your checkboxes in an array, then create a function to iterate over the array and see which box was selected (you can use "for each" and "if"). Add all selected ones to a new array and use this as the function's return value.




回答2:


You should enumerate checkboxes with progressive index, so you can cycle them with a simple for. For example:

//Your checkboxes
var cb1:CheckBox = new CheckBox();
addChild(cb1);
var cb2:CheckBox = new CheckBox();
addChild(cb2);
var cb3:CheckBox = new CheckBox();
addChild(cb3);
var cb4:CheckBox = new CheckBox();
addChild(cb4);
var cb5:CheckBox = new CheckBox();
addChild(cb5);
...

private function getSelectedCb():Array
{
    var returnArray:Array = new Array();
    for(var i:uint = 1; i < 6; i++)
    { 
        var c:CheckBox = this["cb" + i] as CheckBox;
        if(c != null && c.selected)returnArray.push(c);
    }
    return returnArray;
}

Function getSelectedCb() return array containing all selected checkboxes.

I hope this could be usefull to you!



来源:https://stackoverflow.com/questions/7955198/checkbox-as3-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!