Alert is triggered multiple times

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-12 06:24:36

问题


The following code works in the following way:

When I enter incorrect values in the txtother.textbox and focuses out, an alert will be displayed as "Please enter a valid Format Mask." . After pressing the "ok" button in the Alertbox the txtOther.focusout even is triggered again. i.e. immediately after pressing the OK of alert, the same ALERT is displayed again.

I have added the code for ur reference:

//in mxml File:
<mx:Canvas label="General" >
<mx:VBox>
<mx:Canvas id="cvsGeneral"> 
<mx:TextInput id="txtOther" focusOut="txtOther_Validate();"/>
</mx:Canvas>
</mx:VBox>                              
</mx:Canvas>

<mx:Canvas width="100%" height="5%" horizontalScrollPolicy="off" verticalScrollPolicy="off">
<mx:HBox width="80%" height="100%" horizontalAlign="left">
<mx:Button width="64" label="Save" id="btnSave" click="btnSave_Click();" focusIn="txtOther_Validate();"/>
</mx:HBox>
</mx:Canvas>


//Validating action script is as follows:
public function txtOther_Validate():void{
    var formatMask: String = null;  
        if(txtOther.editable && txtOther.enabled){
            if(txtOther.text != ""){
                formatMask = txtOther.text;
                if(conditions to validate){
                    //some expression
                }               
                if(formatMask.length < 12){
                    Alert.show("Please enter format mask with minimum 12 digits.");
                    txtOther.setFocus();
                    return;
                }               VariableEditControl.getInstance().validateFormatMask(txtOther.text,validateFormatMask_Result,validateFormatMask_Fault, validateFormatMask_Error);
            }
        }   
}
public function validateFormatMask_Result(event:PLEvent): void {
    var result:String = event.getData().toString(); // here lets assume that the result variable is stored as "FAILURE"
    if(result == "FAILURE"){
        Alert.show("Please enter a valid Format Mask.");
        txtOther.setFocus(); //
    }
}

I don't want to the alert to come again and again .. I need it in such a way that when the ok button of alert is pressed. The txtother.text should be in focus, and the alert should not come again and again as before.


回答1:


it's because you have the focusIn="txtOther_Validate();" on the ok button a second time. just remove the focusIn handler and you should be fine.




回答2:


I may be in error but I think the alert box won't wait till you close it. So you trigger your validate and immediately set the focus back to the input. Now you click the alert box's ok button which will make the input lose the focus triggering the validation which will raise the alert which will... and so on.




回答3:


EDIT:

stage.focus = txtOther;

This will works in as3... I don't know about flex.



来源:https://stackoverflow.com/questions/7006140/alert-is-triggered-multiple-times

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