在Flash(AS3)中我创建了一个联系表单,其中我有一个“提交按钮”,一个“重置按钮”,四个输入文本字段 “名称,电子邮件,主题,消息”,实例名称为“contact_name,contact_email,contact_subject,contact_message”
我对setFocus和killFocus非常困惑。
在AS3中,我已经给出了
contact_name.text = "Name";
contact_email.text= "Email";
contact_subject.text = "Sub";
contact_message.text = "Message";
现在,当我发布此文件时,默认输入文本字段显示为文本 “姓名,电子邮件,子,消息”
现在我的要求是,如果我点击名称字段,那么文本“名称”应该消失,以便我可以输入我的名字,之后我点击电子邮件字段 文本“电子邮件”应该消失,同时我不想丢失在名称字段中输入的姓名。
如果我点击主题文字字段,文字“Sub”应该消失,但不会丢失我输入的姓名和电子邮件数据。
如果我点击消息文本字段,文本“消息”应该消失,但我不想丢失在剩余文本字段中输入的数据。
请参阅下面的操作脚本。 请帮我找到解决方案。
动作脚本:
contact_name.text ="Name";
contact_email.text ="Email";
contact_subject.text = "Sub";
contact_message.text ="Message";
message_status.text = "";
send_button.addEventListener(MouseEvent.CLICK, submit);
reset_button.addEventListener(MouseEvent.CLICK, reset);
var timer:Timer;
var var_load:URLLoader = new URLLoader;
var URL_request:URLRequest = new URLRequest( "send_email.php" );
URL_request.method = URLRequestMethod.POST;
function submit(e:MouseEvent):void
{
if( contact_name.text == "Name" || contact_email.text == "Email" ||
contact_subject.text == "Sub" || contact_message.text == "Message" )
{
message_status.text = "Please fill up all text fields.";
}
else if( !validate_email(contact_email.text) )
{
message_status.text = "Please enter the valid email address.";
}
else
{
message_status.text = "sending...";
var email_data:String = "name=" + contact_name.text
+ "&email=" + contact_email.text
+ "&subject=" + contact_subject.text
+ "&message=" + contact_message.text;
var URL_vars:URLVariables = new URLVariables(email_data);
URL_vars.dataFormat = URLLoaderDataFormat.TEXT;
URL_request.data = URL_vars;
var_load.load( URL_request );
var_load.addEventListener(Event.COMPLETE, receive_response );
}
}
function reset(e:MouseEvent):void
{
contact_name.text ="Name";
contact_email.text ="Email";
contact_subject.text = "Sub";
contact_message.text ="Message";
message_status.text = "";
}
function validate_email(s:String):Boolean
{
var p:RegExp = /(\w|[_.\-])+@((\w|-)+\.)+\w{2,4}+/;
var r:Object = p.exec(s);
if( r == null )
{
return false;
}
return true;
}
function receive_response(e:Event):void
{
var loader:URLLoader = URLLoader(e.target);
var email_status = new URLVariables(loader.data).success;
if( email_status == "yes" )
{
message_status.text = "Success! Your message was sent.";
timer = new Timer(500);
timer.addEventListener(TimerEvent.TIMER, on_timer);
timer.start();
}
else
{
message_status.text = "Failed! Your message cannot sent.";
}
}
function on_timer(te:TimerEvent):void
{
if( timer.currentCount >= 10 )
{
contact_name.text = contact_email.text = contact_subject.text =
contact_message.text = message_status.text = "";
timer.removeEventListener(TimerEvent.TIMER, on_timer);
}
}
由于 --vamsi
答案 0 :(得分:0)
假设您的contact_name
是Button
,您可以添加一个事件监听器,当用户点击此字段时,该监听器会重置text
属性。类似的东西:
contact_name.addEventListener(
MouseEvent.CLICK,
function(e:Event):void {
contact_name.text = '';
}
);
答案 1 :(得分:0)
我会使用focusIn事件而不是点击,因为在textfields中进行选项卡不会清空它们... 此外,当您从文本字段中将focusOut留空时,它应该重新填充其标签文本...
var defaultText="Name";
addEventListener(FocusEvent.FOCUS_IN, function() {
if(text==defaultText) {
text="";
}
}
addEventListener(FocusEvent.FOCUS_OUT, function() {
if(text=="") {
text=defaultText;
}
}