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

标签 javascript jquery html

我有这个 JavaScript 函数。我想在 div 加载时显示图像加载。如果 div 已加载或出现错误,则将 loadking 图像设置为 null:

现在,加载图像一直显示在页面上,您知道这里缺少什么吗?

$(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');
    });

#loading div 已包含来自 HTML 的图像。

关于javascript - 如何使用 javascript 启用和禁用图像加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25316255/

相关文章:

javascript - 像素完美的碰撞排斥/旋转响应

javascript - jQuery 从 onChange 行选择数据到选项菜单

javascript - 简单的 Javascript show div 函数不起作用

html - 使用 jquery mobile 绘制垂直线和水平线

javascript - 通过主题标签拆分字符串并使用 jQuery 保存到数组中?

javascript - styled-components 'css' 未定义 no-undef

javascript - javascript构造函数的细节

javascript - Angular.js 在数组创建/迭代方面比 jQuery 慢吗?

javascript - 数据属性的数据表搜索

java - Selenium 网络驱动程序 : IE can't find radio buttons nor checkboxes for using click() on them