2016-07-06 25 views
0

我的代碼是這樣的我從教程網站拿它不記得在哪裏請告訴我在哪裏把加載圖標放在這個巨大的垃圾代碼我不明白。 請通過jsfiddle或其他任何地方向我展示一個例子。 如何把這個ajax形式的加載圖標,請任何人告訴我?

<script language = "javascript" type = "text/javascript"> 
    <!-- 
     //Browser Support Code 
     function ajaxFunction(){ 
      var ajaxRequest; // The variable that makes Ajax possible! 

      try { 
       // Opera 8.0+, Firefox, Safari 
       ajaxRequest = new XMLHttpRequest(); 
      }catch (e) { 
       // Internet Explorer Browsers 
       try { 
       ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP"); 
       }catch (e) { 
       try{ 
        ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP"); 
       }catch (e){ 
        // Something went wrong 
        alert("Your browser broke!"); 
        return false; 
       } 
       } 
      } 

      // Create a function that will receive data 
      // sent from the server and will update 
      // div section in the same page. 

      ajaxRequest.onreadystatechange = function(){ 
       if(ajaxRequest.readyState == 4){ 
       var ajaxDisplay = document.getElementById('ajaxDiv'); 
       ajaxDisplay.innerHTML = ajaxRequest.responseText; 
       } 
      } 

      // Now get the value from user and pass it to 
      // server script. 

      var age = document.getElementById('age').value; 
      var wpm = document.getElementById('wpm').value; 
      var sex = document.getElementById('sex').value; 
      var queryString = "?age=" + age ; 

      queryString += "&wpm=" + wpm + "&sex=" + sex; 
      ajaxRequest.open("GET", "ajax-example.php" + queryString, true); 
      ajaxRequest.send(null); 
     } 
    //--> 
    </script> 

    <form name = 'myForm'> 
    Max Age: <input type = 'text' id = 'age' /> <br /> 
    Max WPM: <input type = 'text' id = 'wpm' /> 
    <br /> 

    Sex: <select id = 'sex'> 
     <option value = "m">m</option> 
     <option value = "f">f</option> 
    </select> 

    <input type = 'button' onclick = 'ajaxFunction()' value = 'Query MySQL'/> 

    </form> 

    <div id = 'ajaxDiv'>Your result will display here</div> 

回答

1

首先,檢查this出來的就緒狀態的詳細信息。

我會在ajaxRequest.open("GET", "ajax-example.php" + queryString, true);之前加載一個,並在if (xmlhttp.readyState==4上刪除它。

所以添加HTML您希望加載圖標出現: <span id="loading"></span>

2.

然後之前ajaxRequest.open...插入加載圖片:

document.getElementById("loading").innerHTML = '<img src="loading.gif" />';

而且裏面放if (xmlhttp.readyState == 4

document.getElementById("loading").innerHTML = '';

相關問題