如何启用图像加载和禁用它使用javascript

how do you enable image loading and disabling it using javascript

本文关键字:javascript 加载 图像 何启用 启用      更新时间:2023-09-26

我有这个javascript函数。我想在div加载时显示图像加载。并设置loadking图像为null,如果div被加载或有一个错误:

就像这样,加载图像继续显示在页面上,任何想法我在这里缺少什么?

$(document).ready(function(){
  $("#submitbutton").on("click", function(){
    //disable the button to prevent multiple clicks
    $("#submitbutton").attr("disabled", "disabled");
    $('#loading').html('<img src="img/loading.gif"> loading...');
    var vcenter = $("#v_name").val();
    vcenter = "'"+vcenter+"'";

    var req = ocpu.rpc("output1", {
      vcenter : vcenter
    }, function(output){
      var data=output;
      });
      req.fail(function(){
        alert("Server error: " + req.responseText);
        $('#loading').html();
      });
      //after request complete, re-enable the button 
      req.always(function(){
        $("#submitbutton").removeAttr("disabled");
         $('#loading').html();
      });
    });
  });
html代码:

<button id="submitbutton" type="button">Submit</button>
  <div id="loading"></div>
  <div id="output"> </div>

你需要清空它。

$('#loading').html('');

这就是你当前问题的解决方案。但是我建议这样写:

    $(document).ajaxStart(function () {
        $('#loading').css('display', 'block');
    }).ajaxStop(function () {
        $('#loading').css('display', 'none');
    });

#loadingdiv包含来自HTML的图像。