我在flash中开发一个简单的程序......我遇到了一个复选框的问题......我已经在这里搜索过,我找到了一个主题,说明我需要什么,但我不明白如何这样做。
这是个问题 Checkbox as3 function
与此问题类似...我需要验证我的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]);
}
}
}
答案 0 :(得分: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");
}
答案 1 :(得分: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");
}
}