如何防止Bootstrap 4警报在页面加载时显示?

时间:2019-01-31 19:47:10

标签: javascript asp.net-mvc bootstrap-4 alert

我正在用MVC编写应用程序,并在用户向其订单中添加对象时将成功消息传递给自动解除警报。但是,我将警报设置为最初隐藏,但仍在页面加载时显示。

在JS代码段中指定的时间内,警报仍会在页面加载时显示。

    @if (!string.IsNullOrEmpty(Convert.ToString(Session["UpdResult"])))
    {
        <div id="DivMsg" class="alert alert-success agenda-update">
            @if(Convert.ToString(Session["UpdResult"]).Contains("Err"))
            {
            <div class="alert alert-danger agenda-update text-strong text-center" role="alert" aria-hidden="true">
                <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span>&times;</span></button>
                Sorry, something went wrong. Please
                <a href="#" target="_blank">contact us</a>
                to help with your sessions.
            </div>

            }
            else
            {
            <div class="alert alert-success agenda-update text-strong text-center" role="alert" aria-hidden="true">
                <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span>&times;</span></button>
                @Convert.ToString(Session["UpdResult"])
            </div>                                                
            }
        </div>

    }
    <!-- End Success & Error Messaging -->

最初隐藏警报的JavaScript

@section Scripts {
    <script type="text/javascript">
        window.setTimeout(function () {
            $(".alert").hide();
            $(".alert").fadeTo(500, 0).slideUp(500, function () {
                $(this).remove();
            });
        }, 4000);
    </script>
}

0 个答案:

没有答案