1

嗨,我只是在学习动作脚本,并且一直在尝试在填写文本字段后获得一个按钮来运行功能。phoneNum 是文本字段 ID。

//function with var testing the textfield content. 

function sendInfoBtn()
    {
    var lengthOf = phoneNum.text.length;
    if (lengthOf == 10){
    sendInfo.addEventListener(MouseEvent.CLICK, doThisThing, false, 0, true);
    }
}

//I'd really like to just add the lengthOf variable to this changelistener somehow
phoneNum.addEventListener(Event.CHANGE, sendInfoBtn, false, 0, true);

function realFunction(){
//things to do
}
4

2 回答 2

1

更清洁的方法是:

sendInfo.addEventListener(MouseEvent.CLICK, doThisThing, false, 0, true);

private function doThisThing(e:MouseEvent):void
{
   if(phoneNum.text.length >= 10) 
   {
      realFunction();
   }
}

这样您就不需要更改事件,一旦用户单击按钮,只需验证文本字段是否有足够的字符并调用您想要的实际函数。

如果您希望在输入足够的字符之前禁用该按钮,则可以使用 CHANGE 事件:

sendInfo.enabled = false;
phoneNum.addEventListener(Event.CHANGE, phoneNumChange, false, 0, true);

sendInfo.addEventListener(MouseEvent.CLICK, doThisThing, false, 0, true);

private function doThisThing(e:MouseEvent):void
{
   if(phoneNum.text.length >= 10) 
   {
      realFunction();
   }
}

private function phoneNumChange(e:Event):void
{  
    sendInfo.enabled = phoneNumb.text.length >= 10;
}
于 2013-05-26T04:22:08.597 回答
0

从上面的评论中,我假设问题是如何根据输入字段的状态启用/禁用按钮。

这是一些应该说明它的代码:

<?xml version="1.0" encoding="utf-8"?>
<s:Application 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[

        private function onPhoneNumChanged():void
        {
            sendButton.enabled = (phoneNumField.text.length == 10);
        }

        private function onSend():void
        {
            // do send the phone number
        }
        ]]>
    </fx:Script>

    <mx:VBox>

        <s:TextInput id="phoneNumField" change="onPhoneNumChanged();"/>

        <s:Button label="SEND" id="sendButton" enabled="false" click="onSend();"/>

    </mx:VBox>

</s:Application>
于 2013-05-27T07:26:52.140 回答