在actionscript3中需要if-else建议

时间:2014-04-24 21:49:35

标签: string actionscript-3 flash if-statement flash-cs6

function clickButtonHandler(event:MouseEvent):void
{
    var message:Object = new Object();
    message.text = txtMessage.text;
    message.userName = txtUser.text;
    //Posts to this swf
    showMessage(message);
    //Posts to ALL OTHER swf files..
    group.post(message);

}

function showMessage(message:Object):void
{
    output_txt.appendText(message.userName+": "+message.text + "\n");

}

function jsalertwindow(event:MouseEvent):void
{
    var alert:URLRequest = new URLRequest("javascript:alert('Please enter your User name')");
    navigateToURL(alert, "_self");
}

正如您所看到的,有两个函数包含mouseevent。我想用if-else语句发送这些函数。如果用户在文本输入组件中写了一些名称为txtUser的东西,

  

send_btn.addEventListener(MouseEvent.CLICK,clickButtonHandler);

将起作用,否则(如果用户忘记写任何东西)

  

send_btn.addEventListener(MouseEvent.CLICK,jsalertwindow);

会奏效。  还有一个问题我应该使用MouseEvent.CLICK还是MouseEvent.MOUSE_DOWN?谢谢你的建议。

1 个答案:

答案 0 :(得分:1)

为按钮单击指定单个处理程序(MouseEvent.CLICK是要使用的正确事件)并检查字段是否在处理程序中填充:

function clickButtonHandler(event:MouseEvent):void
{
    var message:Object = new Object();

    // Check the field is populated
    if (txtUser.text != "")
    {
        message.text = txtMessage.text;
        message.userName = txtUser.text;

        showMessage(message);
        //Posts to ALL OTHER swf files..
        group.post(message);
    } 
    else 
    {
        // Nothing in the input field, show the alert
        showAlert();
    }
}

function showMessage(message:Object):void
{
    output_txt.appendText(message.userName+": "+message.text + "\n");

}

function showAlert():void
{
    var alert:URLRequest = new URLRequest("javascript:alert('Please enter your User name')");
    navigateToURL(alert, "_self");
}