如何在选择后在html页面上显示加载图像,直到查询完成,当请求完成时隐藏图像

How to display load image on html page after selecting and until query is work and when request is complete hide image ?

本文关键字:图像 查询 隐藏 完成时 请求 加载 选择 html 显示      更新时间:2023-09-26

如何在选择后的html页面上显示加载图像,直到查询成功,当请求完成时隐藏图像

我有一个从数据库获取信息的网页

当用户在上面的下拉列表中选择一个用户时,会执行一个名为"showUser()"的函数。该功能由"onchange"事件触发:

<html>
<head>
<script>
function showUser(str)
{
if (str=="")
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  } 
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
<select name="users" onchange="showUser(this.value)">
<option value="">Select a person:</option>
<option value="1">Peter Griffin</option>
<option value="2">Lois Griffin</option>
<option value="3">Glenn Quagmire</option>
<option value="4">Joseph Swanson</option>
</select>
</form>
<br>
<div id="txtHint"><b>Person info will be listed here.</b></div>
</body>
</html>

上面JavaScript调用的服务器上的页面是一个名为"getuser.PHP"的PHP文件

"getuser.php"中的源代码对MySQL数据库运行查询,并在HTML表中返回结果:

<?php
$q=$_GET["q"];
$con = mysql_connect('localhost', 'peter', 'abc123');
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("ajax_demo", $con);
$sql="SELECT * FROM user WHERE id = '".$q."'";
$result = mysql_query($sql);
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";
while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['FirstName'] . "</td>";
  echo "<td>" . $row['LastName'] . "</td>";
  echo "<td>" . $row['Age'] . "</td>";
  echo "<td>" . $row['Hometown'] . "</td>";
  echo "<td>" . $row['Job'] . "</td>";
  echo "</tr>";
  }
echo "</table>";
mysql_close($con);
?>

*如何在选择后在html页面上显示加载图像,直到查询完成,当请求完成时隐藏图像*

我尝试在<head>标签中使用代码:

<script>
$.ajaxSetup({   
beforeSend: function (XMLHttpRequest) {
     $("#loading").show();
},
complete: function (XMLHttpRequest, textStatus) {
 $("#loading").hide();
}
});
</script>

<div id="loading">
    <p><img src="loading.gif" /> Please Wait</p>
</div>

但它在我的页面上不起作用

使用jquery。

     <head>
 <script src="http://code.jquery.com/jquery-latest.js"></script>
 <script>
  function getuser(str){
     $.ajax({   
      url: "getuser.php?q="+str,
      beforeSend: function (XMLHttpRequest)
      {
         $("#loading").show();
      }
    }).done(function ( data ) {
     $("#txtHint").html(data);
     $("#loading").hide(); });
  }
  $(document).ready(function(){
  $("select[name='users']").change(function () { 
   getuser($("option:selected", this).val()); });
   });
   </script>
   </head>

你的身体应该是这样的。

   <body>
   <select name="users">
<option value="">Select a person:</option>
<option value="1">Peter Griffin</option>
<option value="2">Lois Griffin</option>
<option value="3">Glenn Quagmire</option>
<option value="4">Joseph Swanson</option>
</select>
   </body>