1

我正在用 Flash 开发一个简单的程序……我遇到了复选框问题……我已经在这里搜索过,我发现一个主题说我需要什么,但我不明白该怎么做。

这是问题 Checkbox as3 功能

与此问题类似...我需要验证是否选择了我的 232 个复选框...即使按下按钮或是否选择了按钮。

任何人都可以帮助我吗?

var select:Array = new Array();
var perguntas:Array = new Array("Ch1","Ch2","Ch3","Ch4","Ch5","Ch6","Ch7","Ch8","Ch9","Ch10",);
var resposta1:Array = new Array("1","0","1","0","0","0","0","0","1","1"."0");
var resposta2:Array = new Array("1","1","0","1","0","1","0","1","0","1","1");
var resposta3:Array = new Array("1","0","0","0","0","0","0","0","0","0","0");
var resposta4:Array = new Array("0","1","0","0","0","0","1","0","1","1","0");
var i:Number;
var p1:Number;
var p2:Number;
var p3:Number;
var p4:Number;


    //Create all the Checkbox
    for(i = 0; i < perguntas.length; i++) {
        select[i] = new CheckBox();
        select[i].y = (i * 20);
        **select[i].addEventListener(MouseEvent.CLICK, seleciona);**
        select[i].label = perguntas[i];
        select[i].width = 600;
        addChild(select[i]);
    }

    //Verify if the checkbox are selected.
    function seleciona(e:MouseEvent):void {
        for(var count:Number = 0; count < select.length; count++){      
        if (select[count].selected == true ){
            p1 += Number(resposta1[count]);
            p2 += Number(resposta2[count]);
            p3 += Number(resposta3[count]);
            p4 += Number(resposta4[count]);
            trace(p1);
        }else{
            trace(p1);
            trace(p2);
            trace(p3);
            trace(p4);
        }
    }
}

所以我已经找到了使用按钮事件的解决方案......我很难为每个复选框设置一个事件......所以让我们简化一下

calcula.addEventListener(MouseEvent.CLICK, selecionaTeste);
function selecionaTeste(e:MouseEvent):void {
    for(var count:Number = 0; count < select.length; count++){      
        if (select[count].selected == true ){
            p1 += Number(resposta1[count]);
        }
    }
}
4

2 回答 2

0

如果您只需要检查是否选择了所有这些,您可以执行以下操作:

var allSelected:Boolean = true;

for(var i=0; i<select.length;i++)
{
 if (select[i].selected == false)
 {
   allSelected = false;
 }
}

if(allSelected == true)
{
  trace("All Checkboxes are Selected!");
}
else
{
  trace("Some or All Checkboxes are left unchecked");
}
于 2012-12-11T20:38:25.250 回答
0

如果有人想要检查特定的复选框。我们正在检查的复选框的实例名称为“chk_demo”。在这里,我设置了一个全局变量,以便可以从其他函数/框架中检查它。添加 event.CHANGE 将在变量被选中或取消选中时更新它。我在 CS5.5 中使用 AS3

var isDemo = '0'; // set to 0 since it is not selected initially
chk_demo.addEventListener(Event.CHANGE, demo); // Run function when checked or unchecked (dynamically)

function demo(event:Event = null):void {
    if (chk_demo.selected == true) {
        isDemo = '1';
        trace("Box is checked");
    } else {
        isDemo = '0';
        trace("Box is NOT checked");
    }
}
于 2020-02-28T18:23:42.700 回答