javascript - How to show loading image before Action is completed ASP.Net MVC 5 - Stack Overflow

This is an ASP.Net MVC 5 project.I have a simple javascriptjQuery as follow:<script>$(document).

This is an ASP.Net MVC 5 project.

I have a simple javascript/jQuery as follow:

<script>
  $(document).ready(function () {
    $("#textBox").focusout(function () {
      var phrase= $("#textBox").val();
      phrase= phrase.replace(new RegExp(/\s+/, 'g'), "%20");
      $("#mentDiv").load("../Search/SearchSingular?phrase=" + phrase);
    });
  });
</script>

As you can see, there is a jQuery .load method which calls SearchSingular action in the Search controller when the focus is out from HTML element with id = textBox. This works well, except that the SearchSingular action execution may sometimes take time to finish.

Thus, I want to show a loading image when the load is not finished. Is there any way to do this?

This is an ASP.Net MVC 5 project.

I have a simple javascript/jQuery as follow:

<script>
  $(document).ready(function () {
    $("#textBox").focusout(function () {
      var phrase= $("#textBox").val();
      phrase= phrase.replace(new RegExp(/\s+/, 'g'), "%20");
      $("#mentDiv").load("../Search/SearchSingular?phrase=" + phrase);
    });
  });
</script>

As you can see, there is a jQuery .load method which calls SearchSingular action in the Search controller when the focus is out from HTML element with id = textBox. This works well, except that the SearchSingular action execution may sometimes take time to finish.

Thus, I want to show a loading image when the load is not finished. Is there any way to do this?

Share Improve this question asked Sep 9, 2016 at 5:12 IanIan 30.9k20 gold badges76 silver badges112 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

Turn it into a callback

// Somecode to display a image

$("#mentDiv").load("../Search/SearchSingular?phrase=" + phrase, function() {
  // Somecode to remove image
});

You can use a loading image while div contents load

("#loadingImage").show();
$("#mentDiv").load("../Search/SearchSingular?phrase=" + phrase, function(){

  $("#loadingImage").hide(); // hide the image after loading of div pletes

});

Div for your loading image

<div id="loadingImage">
 <img src="loader.gif">
</div>

There is a plete callback of jQuery .load function (read more http://api.jquery./load/)

You could revise your javascript as following:

<script>
  $(document).ready(function () {
    $("#textBox").focusout(function () {
      var phrase= $("#textBox").val();
      phrase= phrase.replace(new RegExp(/\s+/, 'g'), "%20");
      ShowLoading();
      $("#mentDiv").load(
        "../Search/SearchSingular?phrase=" + phrase,
        function () { HideLoading(); }
      );
    });
  });
</script>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745186624a4615668.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信