如何在 ajax 操作期间显示 throbber 或其他指示器?

发布于 2024-11-02 20:44:11 字数 250 浏览 0 评论 0原文

我想知道如何在“远程”功能忙于后端程序时显示ajax loader gif

如果可能的话,您能否查看牛奶示例并告诉我它如何适合进入该代码。只需单击“显示此页面上使用的脚本”即可查看源代码。

谢谢

I would like to know how to show an ajax loader gif while the "remote" function is busy with backend procedures.

If possible, could you please look at the milk example and tell me how it would fit into that code. Simply click on "Show script used on this page" to view the source.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

往事风中埋 2024-11-09 20:44:11

假设您已经知道如何使用 AJAX。基本过程是,在操作开始时显示图像,最后当从服务器返回结果时隐藏它。

<!--html-->
<img id='ajaxLoader' src='mahAjaxLoader.gif' />
<!--has display:none via CSS-->

-

//js
function doAjaxStuff() {
    $('#ajaxLoader').toggle(); //toggle visibility; it's now shown

    //other stuff

    $.ajax({
        //normal AJAX stuff
        onComplete : function() {
            $('#ajaxLoader').toggle(); //it's hidden again
            //other oncomplete stuff
    });
}

请参阅:

编辑: remote 方法像 ajax 请求一样接受对象文字。所以插入这个:

remote : {
    beforeSend : function() {
        $('#ajaxLoader').toggle();
    }
    onComplete : function() {
        $('#ajaxLoader').toggle();
    }
}

Assuming you already know how to AJAX. The basic procedure is that at the beginning of the operation you show an image, and at the end when you get results back from the server you hide it.

<!--html-->
<img id='ajaxLoader' src='mahAjaxLoader.gif' />
<!--has display:none via CSS-->

-

//js
function doAjaxStuff() {
    $('#ajaxLoader').toggle(); //toggle visibility; it's now shown

    //other stuff

    $.ajax({
        //normal AJAX stuff
        onComplete : function() {
            $('#ajaxLoader').toggle(); //it's hidden again
            //other oncomplete stuff
    });
}

See:

Edit: The remote method accepts an object literal like the ajax request does. So plug this in:

remote : {
    beforeSend : function() {
        $('#ajaxLoader').toggle();
    }
    onComplete : function() {
        $('#ajaxLoader').toggle();
    }
}
ぽ尐不点ル 2024-11-09 20:44:11

对于您的 AJAX 调用:

var GetWSDataJSON = function (ServiceUrl, Parameters, onSuccess, onFailure, onComplete){
    $.ajax({
        type: "POST",
        data: "{" + Parameters + "}",
        url: basePath + ServiceUrl,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (result) {
            var msg = result.d;
            onSuccess(msg);
        },
        error: function (request, status, throwerror) {
            onFailure();
        },
        complete: function () {
            if (onComplete != undefined) {
            onComplete();
        }
     }
  });
}

然后,在您的实际 AJAX 请求中:

function SomeFunctionHere() {
    $('#containerDiv').showLoading();
    GetWSDataJSON('WebServices/Service.asmx/GetYourData', 'someData: "' + someVar + '"', SomeFunction_Success, SomeFunction_Failure, SomeFunctionComplete);
}

最后,AJAX 调用后的函数:

var SomeFunction_Success = function(msg){ //do something with the msg }
var SomeFunction_Failure = function(){ //do something with the error }
var SomeFunction_Complete = function(){ $('#containerDiv').hideLoading(); }

For your AJAX call:

var GetWSDataJSON = function (ServiceUrl, Parameters, onSuccess, onFailure, onComplete){
    $.ajax({
        type: "POST",
        data: "{" + Parameters + "}",
        url: basePath + ServiceUrl,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (result) {
            var msg = result.d;
            onSuccess(msg);
        },
        error: function (request, status, throwerror) {
            onFailure();
        },
        complete: function () {
            if (onComplete != undefined) {
            onComplete();
        }
     }
  });
}

Then, in your actual AJAX request:

function SomeFunctionHere() {
    $('#containerDiv').showLoading();
    GetWSDataJSON('WebServices/Service.asmx/GetYourData', 'someData: "' + someVar + '"', SomeFunction_Success, SomeFunction_Failure, SomeFunctionComplete);
}

Finally, your functions for after the AJAX call:

var SomeFunction_Success = function(msg){ //do something with the msg }
var SomeFunction_Failure = function(){ //do something with the error }
var SomeFunction_Complete = function(){ $('#containerDiv').hideLoading(); }
热鲨 2024-11-09 20:44:11

我从您创建的页面中获取了此内容。为了简单起见,我将其全部作为 js,但您可以使用 html 创建元素并应用样式表中的样式。

$(document).ready(function(){
    $("#signupwrap").prepend("<div id='ajax_loader'><img src='path_to_image' /></div>");
    $("#ajax_loader").css({
        width: $("#signupwrap").width(),
        height: $("#signupwrap").height()
    }).hide();

    $("#ajax_loader img").css({
        position: "absolute",
        top:"300px" //or wherever you want to put it
        left:"50%",
        marginLeft:$(".ajax_loader img").width()/2
    });
});


submitHandler: function() {
    alert("submitted!");
    var dataObj = "sdfsd" // get all the values and build the ajax data object
    $("#ajax_loader").show();
    $.ajax({
        url:"your_url",
        data: dataObj,
        success:function(val){
            $("#ajax_loader").hide();

        };
    });
},

I took this from the page you created. For simplistic sake I put it all as js but you can create the element with html and apply the styles in the style sheet.

$(document).ready(function(){
    $("#signupwrap").prepend("<div id='ajax_loader'><img src='path_to_image' /></div>");
    $("#ajax_loader").css({
        width: $("#signupwrap").width(),
        height: $("#signupwrap").height()
    }).hide();

    $("#ajax_loader img").css({
        position: "absolute",
        top:"300px" //or wherever you want to put it
        left:"50%",
        marginLeft:$(".ajax_loader img").width()/2
    });
});


submitHandler: function() {
    alert("submitted!");
    var dataObj = "sdfsd" // get all the values and build the ajax data object
    $("#ajax_loader").show();
    $.ajax({
        url:"your_url",
        data: dataObj,
        success:function(val){
            $("#ajax_loader").hide();

        };
    });
},

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文