javascript - 在此ajax表单中将加载图标放在哪里请有人告诉我吗?

标签 javascript php jquery ajax

我的代码是这样的,我从教程网站上获取它,不记得在哪里,请告诉我在这个我不理解的巨大代码垃圾中将加载图标放在哪里。 请通过 jsfiddle 或其他任何地方向我展示一个示例。

  <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>

最佳答案

首先,检查this了解有关就绪状态的更多信息。

我会在 ajaxRequest.open("GET", "ajax-example.php" + queryString, true); 之前添加一个加载内容并将其删除 if (xmlhttp.readyState==4 .

1.

因此,在您希望显示加载图标的位置添加 HTML: <span id="loading"></span>

2.

然后就在 ajaxRequest.open... 之前插入加载图像:

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

3.

里面if (xmlhttp.readyState == 4把:

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

关于javascript - 在此ajax表单中将加载图标放在哪里请有人告诉我吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38214808/

相关文章:

jquery - 当页面被压缩时,Bootstrap Top Glyph 链接将不起作用

javascript - 对于帖子请求,pace-rails 不显示

javascript - 添加前清除 div

javascript - react native : Inline image with text

javascript - Angular 错误 : [$injector:unpr] while adding ui. Bootstrap

php - 如何显示 Zend 框架抛出的异常错误?

php - 在 php 中组织技术和面向用户的错误消息

java - 即使没有必要,if 子句是否也会遍历其所有语句?

javascript - 使用 OnkeyDown 作为 if 条件 Javascript

php - 根据php数组生成php源码