我有一个表单,在用户通过提交按钮发送消息后,会给出“成功发送”消息。
但是一秒钟后,带有表单的原始页面再次显示,因为提交使页面刷新。
代码是:
<label id="success" style="visibility: hidden">Your request has been sent successfully</label>
<form method="post" id="messege" onsubmit="return checkAnswer()" style="font-size: medium; margin-top: 10%" dir="rtl">
<div class="clear"></div>
<div class="content_item">
<br><br>
<div style="width:170px; float: right;">name</div>
<div style="width:430px; float:left;"><p><input class="contact" type="text" id="your_name" value="" /></p></div>
<br><br><br><br><br>
<div style="width:170px; float:right;">email</div>
<div style="width:430px; float:left;"><p><input class="contact" type="text" id="your_email" value="" /></p></div>
<br><br><br>
<div style="width:170px; float:right;">the messege</div>
<div style="width:430px; float:left;"><p><textarea class="contact textarea" rows="8" cols="50" id="your_message"></textarea></p></div>
<br style="clear:both;" />
</div>
</form>
javascript函数是:
<script type="text/javascript">
function checkAnswer() {
var name = document.getElementById("your_name").value;
if (name == "") {
alert("!!! insert name");
return false;
}
var email = document.getElementById("your_email").value;
if (email == "") {
alert("!!! insert mail");
return false;
}
var message = document.getElementById("your_message").value;
if (message == "") {
alert("!!! insert massege");
return false;
}
var ans = parseInt(document.getElementById("user_answer").value);
if (ans != 12) {
alert("!!! wrong answer");
return false;
}
else {
document.getElementById("success").style.visibility = 'visible';
document.getElementById("messege").style.visibility = 'hidden';
return true;
}
}
而asp.net代码是:
@{
var db = Database.Open("MyProjectSite");
var name="";
var email="";
var message="";
var answer="";
if(IsPost)
{
name=Request.Form["your_name"];
email=Request.Form["your_email"];
message=Request.Form["your_message"];
var insertMessege="INSERT INTO messegesFromCustomers(name,email,content,isCustomer)"+"VALUES (@0,@1,@2,@3)";
db.Execute(insertMessege,name,email,message,"לא");
db.Close();
}
}
当我使用javascript将成功消息的可见属性设置为:“可见”时,如何防止刷新页面?
刷新也删除了我的成功信息。
谢谢