简体   繁体   English

MVC JQuery Ajax忙指示符覆盖在窗体上

[英]MVC JQuery Ajax Busy Indicator overlay on Form

I have created a simple form which is submitted using JQuery Ajax and works very well. 我创建了一个使用JQuery Ajax提交的简单表单,效果很好。 What I am trying to achieve is that a busy indicator "loader image" is displayed on the entire form as an overlay. 我要达到的目的是在整个表单上以覆盖图的形式显示繁忙的指示器“加载程序图像”。 I have seen many example but most of them are related to show and hide a simple div containing the image and it doesn't act as overlay on the form. 我已经看到了很多示例,但是大多数示例都与显示和隐藏包含图像的简单div有关,并且它不充当表单的叠加层。 What I have achieved so far is to just show the loading indicator on the center of the screen with the help of css. 到目前为止,我已经完成的工作就是在CSS的帮助下将加载指示器显示在屏幕中央。 Below is code for my div and css for it. 下面是我的div和CSS的代码。

<div id="spinner">
    <img src="@Url.Content("~/images/loader.gif")" alt="Please wait...">
</div>

and here is the css, 这是CSS,

<style>
  div#spinner {
    display: none;
    position: fixed;
    top: 50%;
    left: 50%;
    background: url(spinner.gif) no-repeat center #fff;
    text-align: center;
    padding: 10px;
    font: normal 16px Tahoma, Geneva, sans-serif;
    border: 1px solid #666;
    margin-left: -50px;
    margin-top: -50px;
    z-index: 2;
    overflow: auto;
  }
</style>

Ajax call might not be important in this scenario but I will put just in case it is needed, 在这种情况下,Ajax调用可能并不重要,但我只在需要时提出,

$(function () {
    $('#RegisterForm').submit(function () {
        if ($(this).valid()) {
            $("div#spinner").fadeIn("fast");
            $.ajax({
                url: this.action,
                type: this.method,
                data: $(this).serialize(),
                success: function (result) {
                    $("div#spinner").fadeOut("fast");
                    $("div#result").html(result);
                }
            });
        }
        return false;
    });
});

Now as I said with this code the loader image is displayed on the center of screen but it is not overlay. 现在,正如我对这段代码所说的,加载程序图像显示在屏幕中央,但未覆盖。 But I want the loader image to be overlayed on the form only. 但是我只希望加载程序图像仅覆盖在表单上。 But it should adjust itself according to form size on runtime because I am not specifying any height. 但是它应该在运行时根据表单大小进行调整,因为我没有指定任何高度。 Is this achievable? 这可以实现吗? If yes then how? 如果是,那怎么办?

What's a little weird is that you have loader.gif and also spinner.gif... I'm guessing you are purposely using both for a reason. 有点奇怪的是,您同时拥有loader.gif和spinner.gif ...我猜您是出于某种原因故意使用这两者。 I'll take that into account. 我会考虑到这一点。 I'm assuming #spinner is a child of #RegisterForm. 我假设#spinner是#RegisterForm的子级。 If that's the case you could have the styles be as follows: 如果是这样,您可以将样式设置如下:

<style>
    #RegisterForm {
        display: block;
        position: relative;
    }
    #spinner {
        display: none;
        position: absolute;
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
        text-align: center;
        background: url(spinner.gif) no-repeat center #fff;
        padding: 10px;
        font: normal 16px Tahoma, Geneva, sans-serif;
        border: 1px solid #666;
        z-index: 2;
        opacity: 0.75;
    }
</style>

What this does is stretch out the spinner DIV to cover all of the #RegisterForm. 这样做是将微调器DIV扩展为涵盖所有#RegisterForm。 I also added the opacity so you can see how it's overlayed. 我还添加了不透明度,因此您可以看到它是如何叠加的。 The JavaScript looks fine and should do the trick. JavaScript看起来不错,应该可以解决问题。

I have found another solution which is simple enough for me and works out of the box. 我找到了另一个对我来说很简单并且可以直接使用的解决方案。 The solution is to use jQuery.BlockUI. 解决方案是使用jQuery.BlockUI。 It can not only block the whole page but also can block any element on the page. 它不仅可以阻止整个页面,还可以阻止页面上的任何元素。 The css is rendered by default but css can be changed if desired. 默认情况下会渲染css,但可以根据需要更改css。 I have added the package from NuGet. 我已经从NuGet添加了软件包。 Added the reference file and updated my Ajax call like this, 添加了参考文件并像这样更新了我的Ajax调用,

$(function () {
    $('#RegisterForm').submit(function () {
        if ($(this).valid()) {
            $("#RegisterForm").block({
                message: '<img src="@Url.Content("~/images/loader.gif")" alt="Please wait...">',
                css: { width: '4%', border: '0px solid #FFFFFF', cursor: 'wait', backgroundColor: '#F2F2F4' },
                overlayCSS: { backgroundColor: '#FFFFFF', opacity: 0.5, cursor: 'wait' }
            });
            $.ajax({
                url: this.action,
                type: this.method,
                data: $(this).serialize(),
                success: function (result) {
                    $("#RegisterForm").unblock();
                    $("div#result").html(result);
                }
            });
        }
        return false;
    });
});

It works like a charm. 它像一种魅力。 I can change the background and loading image too. 我也可以更改背景并加载图像。 So if anyone is looking for the same solution then this might be helpful. 因此,如果有人在寻找相同的解决方案,那么这可能会有所帮助。

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

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