使用条件语句验证AS3中的文本字段

时间:2013-05-31 17:13:28

标签: actionscript-3

我试图隐藏表单上的提交按钮,直到使用checkForm按钮和if语句完成了必填字段。我有问题让它工作,我一直在研究可能的答案,但我被卡住了。我想知道是否有人看看下面的代码,并指出我正确的方向。

submit_btn.visible=false;
checkForm_btn.addEventListener(MouseEvent.CLICK, entryTest)
function entryTest():void{
arguments;
if (name_txt.text != "")
trace("name needs completing");
else if (email_txt.text != "")
trace("email needs completing");
else if (question_txt.text != "")
trace("question needs completing");
else submit_btn.visible=true;
}

以下是我根据一些建议编辑的代码的副本 - 但它仍然无法正常工作。我只收到第一个if语句的输出。

submit_btn.visible=false;
checkForm_btn.addEventListener(MouseEvent.CLICK, entryTest)
function entryTest(event:MouseEvent):void{
  if(name_txt.text != ""){
    trace("name needs completing");
  } else if(email_txt.text != ""){
    trace("email needs completing");
  } else if(question_txt.text != ""){
    trace("question needs completing");
  } else {
    submit_btn.visible = true
  }
}

1 个答案:

答案 0 :(得分:1)

你有语法错误。

else (submit_btn.visible=true);

应该是

else
  submit_btn.visible = true;

要完成,你的按钮事件处理程序应该声明传递的事件所期望的数据类型......并且你可能会发现使用大括号{}来包装你的代码块应该帮助澄清条件是什么以及满足条件时执行的代码是什么。 (它还可以轻松添加/删除更多代码,而无需知道是否需要添加括号)

function entryTest(event:MouseEvent):void{
  if(name_txt.text == ""){
    trace("name needs completing");
  } else if(email_txt.text == ""){
    trace("email needs completing");
  } else if(question_txt.text == ""){
    trace("question needs completing");
  } else {
    submit_btn.visible = true;
  }
}
相关问题