您好我只是在学习动作脚本,并且在填写完文本字段后尝试获取一个按钮来运行函数.choneNum是文本域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
}
答案 0 :(得分: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;
}
答案 1 :(得分: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>