根据其ID或标签以编程方式选择单选按钮

时间:2014-03-13 08:59:15

标签: actionscript-3 flex

如何根据其ID或标签以编程方式选择单选按钮?

<mx:FormItem label="Aménagé : " direction="horizontal" paddingLeft="13">                                            

  <mx:RadioButtonGroup id="ln_amenage" enabled="true" />

  <mx:RadioButton id="Oui" label="oui" groupName="ln_amenage" value="oui"/>

  <mx:RadioButton id="Non" label="non" groupName="ln_amenage" value="non"/>

  <mx:RadioButton id="Encours" label="encours" groupName="ln_amenage" value="encours"/>                                 

</mx:FormItem>

1 个答案:

答案 0 :(得分:0)

这会对你有所帮助。

在mxml标记中

<mx:FormItem id="formItem" label="Aménagé : " direction="horizontal" paddingLeft="13">  
    <mx:RadioButtonGroup id="ln_amenage" enabled="true" />  
    <mx:RadioButton id="Oui" label="oui" groupName="ln_amenage" value="oui"/>   
    <mx:RadioButton id="Non" label="non" groupName="ln_amenage" value="non"/>   
    <mx:RadioButton id="Encours" label="encours" groupName="ln_amenage" value="encours"/>   
</mx:FormItem>

请注意这里为FormItem组件提供id

private function selectRadioButtonByIdOrLabel(idOrLabel:String):void
{
    for (var i:int = 0,len:int = formItem.numChildren; i < len; i++) 
    {
        var item:UIComponent = formItem.getChildAt(i) as UIComponent;

        if(item && item is RadioButton && RadioButton(item).id == idOrLabel || RadioButton(item).label == idOrLabel)
        {
            RadioButton(item).dispatchEvent(new MouseEvent(MouseEvent.CLICK));
        }
    }
}

调用方法如

selectRadioButtonByIdOrLabel("non");