简体   繁体   English

阻止页面刷新以将成功消息保留在页面上

[英]prevent page refresh to keep success message on the page

I have a form that after the user sends his message by submit button, gives a 'success sending' message. 我有一个表单,在用户通过“提交”按钮发送消息后,给出了“成功发送”消息。

But after a second the original page with the form is showing again because submit makes the page refresh. 但是一秒钟后,带有表单的原始页面将再次显示,因为Submit使页面刷新。

The code is: 代码是:

<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> 

The javascript function is: 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;
        }

    }

And the asp.net code is: 而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();
    }

}

When I set the visible property of the success message to:"visible" with javascript, how could I prevent the refreshing of the page? 当我使用javascript将成功消息的visible属性设置为“ visible”时,如何防止刷新页面?

The refresh erases my success message too. 刷新也删除了我的成功消息。

Thanks 谢谢

If you want to submit a form like that and not have the page refresh I think you need to use Ajax. 如果您想提交这样的表单并且不刷新页面,我认为您需要使用Ajax。 The only way to page can submit the form is by refreshing, unless you send the data to your ASP.net code some other way... one way is using Ajax. 页面提交表单的唯一方法是刷新,除非您以其他方式将数据发送到ASP.net代码...一种方法是使用Ajax。 (Another is using an iframe, but Ajax is probably the better option). (另一个正在使用iframe,但是Ajax可能是更好的选择)。

Take a look at this link: 看一下这个链接:

http://net.tutsplus.com/tutorials/javascript-ajax/submit-a-form-without-page-refresh-using-jquery/ http://net.tutsplus.com/tutorials/javascript-ajax/submit-a-form-without-page-refresh-using-jquery/

It shows how to submit a form without refreshing the page using ajax (through jQuery). 它显示了如何提交表单而不使用Ajax(通过jQuery)刷新页面。

Hopefully this helps! 希望这会有所帮助!

在您的情况下,使用此功能的最简单方法是添加url标志以表示提交成功,然后在页面加载时检查它并显示确认消息,而不是在页面提交之前未保存(如您所见)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM