如何在选择后在html页面上显示加载图像,直到查询完成,当请求完成时隐藏图像
How to display load image on html page after selecting and until query is work and when request is complete hide image ?
如何在选择后的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>
相关文章:
- jQuery对请求图像的外部文本进行查询
- 使用媒体查询设置背景图像的不透明度
- j查询动态更改图像标头
- j查询随机图像推子
- j查询日历与mootool图像滑块冲突
- j查询使用动画更改图像并等待它触发超链接
- j查询悬停图像效果
- j查询在使用 JSON 更改 attr src 时如何淡出和淡入图像
- j查询图像预加载顺序
- j查询切换图像不切换
- 显示查询输出 HTML 中的图像
- 将来自 SalesForce 的图像作为 blob 进行查询
- j查询滑块图像居中建议
- 使用查询的水平图像滚动
- j查询更新图像 src 在悬停时
- j查询背景图像 URL 失败
- j查询图像幻灯片不起作用
- j查询切换 - 更改图像的背景位置
- j查询幻灯片切换更改图像
- 简单切换图像查询