我有一个复选框列表,我希望能够一次选择并取消选中它们,我将如何使用复选框执行此操作?
我的复选框的mxml如下:
<mx:VBox>
<mx:CheckBox id="newCB" label="NEW" selected="true" change="onCheckboxChange(event)" />
<mx:CheckBox id="tradeCB" label="TRADE" selected="true" change="onCheckboxChange(event)"/>
<mx:CheckBox id="amendCB" label="AMEND" selected="true" change="onCheckboxChange(event)" />
<mx:CheckBox id="cancelCB" label="CANCEL" selected="true" change="onCheckboxChange(event)" />
</mx:VBox>
答案 0 :(得分:0)
例如,您可以使用按钮选择或取消选中所有复选框,如下所示:
<mx:VBox id="box">
<mx:CheckBox id="newCB" label="NEW" selected="true" change="onCheckboxChange(event)" />
<mx:CheckBox id="tradeCB" label="TRADE" selected="true" change="onCheckboxChange(event)"/>
<mx:CheckBox id="amendCB" label="AMEND" selected="true" change="onCheckboxChange(event)" />
<mx:CheckBox id="cancelCB" label="CANCEL" selected="true" change="onCheckboxChange(event)" />
</mx:VBox>
然后
var num_children:int = box.numChildren;
var selected:Boolean = true;
for(var i:int = 0; i < num_children; i++)
{
var checkbox:CheckBox = CheckBox(box.getChildAt(i));
checkbox.selected = ! selected;
}
修改:
您只需反转selected
,然后:
for(var i:int = 0; i < num_children; i++)
{
var checkbox:CheckBox = CheckBox(box.getChildAt(i));
if(checkbox.selected == selected) checkbox.selected = ! selected;
}
selected = false;
希望可以提供帮助。
答案 1 :(得分:0)
我为您制作了一个小沙箱应用程序,以显示视图状态的使用。在按钮上单击它会更改状态,并选中复选框&#39; selected
属性可以绑定到州。
使用状态
<s:layout>
<s:VerticalLayout/>
</s:layout>
<s:Button click="onClick()" label="{this.currentState=='check'?'uncheck':'check'}"/>
<mx:VBox>
<mx:CheckBox id="newCB" label="NEW" selected="true" selected.check="true" selected.uncheck="false"/>
<mx:CheckBox id="tradeCB" label="TRADE" selected="true" selected.check="true" selected.uncheck="false"/>
<mx:CheckBox id="amendCB" label="AMEND" selected="true" selected.check="true" selected.uncheck="false"/>
<mx:CheckBox id="cancelCB" label="CANCEL" selected="true" selected.check="true" selected.uncheck="false"/>
</mx:VBox>
</s:WindowedApplication>
使用循环
<?xml version="1.0"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx">
<fx:Script><![CDATA[
[Bindable]
private var checkAll:Boolean = false;
public function onClick():void {
for each(var c:CheckBox in checkboxes.getChildren()){
c.selected = checkAll;
}
checkAll = !checkAll;
}
]]></fx:Script>
<s:layout>
<s:VerticalLayout/>
</s:layout>
<s:Button click="onClick()" label="{checkAll?'check all':'uncheck all'}"/>
<mx:VBox
id="checkboxes">
<mx:CheckBox id="newCB" label="NEW" selected="true"/>
<mx:CheckBox id="tradeCB" label="TRADE" selected="true" />
<mx:CheckBox id="amendCB" label="AMEND" selected="true" />
<mx:CheckBox id="cancelCB" label="CANCEL" selected="true"/>
</mx:VBox>
</s:WindowedApplication>