Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
479 views
in Technique[技术] by (71.8m points)

javascript - 出现某些情况时显示警报错误消息(Show alert error message when certain condition arises)

I have the following code on page load that I use to show an error alert:(我在页面加载时有以下代码,用于显示错误警报:)

protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { if (Request.QueryString["message"] == "noemployees") AlertDanger.Visible = true; This is where the error is called.(这是调用错误的地方。) The page reloads and the error is shown.(页面重新加载,并显示错误。) if (payFitments == null) { Response.Redirect("Default?message=noemployees"); } I have the following markup(我有以下标记) <script type="text/javascript"> $(document).ready(function () { window.setTimeout(function () { $(".alert").fadeTo(1500, 0).slideUp(500, function () { $(this).remove(); }); }, 3000); }); ------------------------- <div runat="server" visible="false" id="AlertDanger" class="alert alert-danger"> <a href="#" class="close" data-dismiss="alert">&times;</a> <strong>You must have at least one employee to process</strong> </div> How do I show this message without having to load the default page again?(如何显示此消息而不必再次加载默认页面?) Not seeing examples on the web that show this clearly.(在网络上没有看到清楚地表明这一点的示例。)   ask by Kinyanjui Kamau translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You're using JQuery remove , which effectively deletes the alert from your DOM.(您正在使用JQuery remove ,它可以有效地从DOM中删除警报。)

So showing it again without reloading your page is not possible.(因此,无法重新显示而不重新加载页面是不可能的。) But you could use JQuery detach instead.(但是您可以改用JQuery detach 。) Then you can insert it again with appendTo .(然后,您可以使用appendTo再次将其插入。) var alert = null; function showHideAlert() { if (alert) { alert.appendTo("body"); alert = null; } else { alert = $(".alert").detach(); } } <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <body> <button onclick="showHideAlert();">Show/Hide alert</button> <div class="alert">ALERT!!!</div> </body>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...