douque9815 2013-09-07 04:23
浏览 49
已采纳

当ajax正在进行时,ajax显示加载图像

I have ajax code where I am getting the products name from a store. Its working absolutely fine. But I want that when the ajax process is in continue it should show the loading image. My ajax code is like this My onClick button is like this.

<input type="button"  onclick="getProdList(this.value,this.id)">

My getProdList function is like this

function getProdList(id,langid) {
  var xmlHttp=initXMLHTTPRequest();
  //console.log(xmlHttp);
  var str = "value="+id+"&langid="+langid;
  var span="";

  var url = "../php/searchDetails.php?";
    document.getElementById('imgDiv').style.display = 'block';
  xmlHttp.onreadystatechange=function() {
  if(xmlHttp.readyState==4 ) {
    document.getElementById('imgDiv').style.display = 'none';
    span=span+"<ul style='padding-bottom:20px;'><li><b>"+id+"</b></li>";
    var xmldata=xmlHttp.responseXML;
    var xmlObj = xmldata.getElementsByTagName("ProductDetails")[0];
    var menusize=xmlObj.childNodes[0].childNodes[0].childNodes[0].nodeValue;
    var xmlObjlength = xmlObj.childNodes.length;
    for(var i=1;i<xmlObjlength;i++) {
      var ProductName=xmlObj.childNodes[i].childNodes[0].childNodes[0].nodeValue;
      var productId=xmlObj.childNodes[i].childNodes[1].childNodes[0].nodeValue;
      span=span+"<li  style='line-height:20px;display:block;height:20px;list-style:none;border-bottom:1px solid #666666;'><a href='product.php?id_product="+productId+"'><div>"+ProductName+"</div></a></li>"
    }
    if(xmlObjlength==1) {
      document.getElementById("products").innerHTML="";
      document.getElementById("products").innerHTML="Sorry No Products Under this Alphabet";
    }
    else {
      document.getElementById("products").innerHTML="";
      span=span+"</ul>";
      document.getElementById("products").innerHTML=span;
    }
  }
  };
  xmlHttp.open("POST",url,true); 
  xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  xmlHttp.setRequestHeader("Content-length", str.length);
  xmlHttp.setRequestHeader("Connection", "close");
  xmlHttp.send(str);
}

Any help and suggestions will be really appreciable. Thanks

  • 写回答

1条回答 默认 最新

  • dtz30833 2013-09-07 04:26
    关注
    function getProdList(id,langid) {
      var xmlHttp=initXMLHTTPRequest();
      //console.log(xmlHttp);
      var str = "value="+id+"&langid="+langid;
      var span="";
      var url = "../php/searchDetails.php?";
    
      document.getElementById('imgDiv').style.display = 'block';
    
      xmlHttp.onreadystatechange=function() {
      if(xmlHttp.readyState==4 ) {
    
        document.getElementById('imgDiv').style.display = 'none';
    
        span=span+"<ul style='padding-bottom:20px;'><li><b>"+id+"</b></li>";
        var xmldata=xmlHttp.responseXML;
        var xmlObj = xmldata.getElementsByTagName("ProductDetails")[0];
        var menusize=xmlObj.childNodes[0].childNodes[0].childNodes[0].nodeValue;
        var xmlObjlength = xmlObj.childNodes.length;
        for(var i=1;i<xmlObjlength;i++) {
          var ProductName=xmlObj.childNodes[i].childNodes[0].childNodes[0].nodeValue;
          var productId=xmlObj.childNodes[i].childNodes[1].childNodes[0].nodeValue;
          span=span+"<li  style='line-height:20px;display:block;height:20px;list-style:none;border-bottom:1px solid #666666;'><a href='product.php?id_product="+productId+"'><div>"+ProductName+"</div></a></li>"
        }
        if(xmlObjlength==1) {
          document.getElementById("products").innerHTML="";
          document.getElementById("products").innerHTML="Sorry No Products Under this Alphabet";
        }
        else {
          document.getElementById("products").innerHTML="";
          span=span+"</ul>";
          document.getElementById("products").innerHTML=span;
        }
      }
      };
      xmlHttp.open("POST",url,true); 
      xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      xmlHttp.setRequestHeader("Content-length", str.length);
      xmlHttp.setRequestHeader("Connection", "close");
      xmlHttp.send(str);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度