这几天在做PHP+Ajax无刷新联动菜单,IE下正常但火狐不正常,高手请帮忙看下。
代码如下:
<? include('../admin/config.php'); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
<html xmlns=" http://www.w3.org/1999/xhtml" >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>PHP+Ajax二级联动菜单</title>
<script language="javascript" src="iniajax.js"> </script>
<script language="javascript">
function gettext(){
document.getElementById("test").value=document.getElementById("SecondCategory").value;
}
</script>
<style type="text/css">
option {
color:#993333;
}
input {
color:#993333;
}
</style>
</head>
<body>
<select id="FirstCategory" onchange="ShowCategory()">
<option selected="selected" value=""> 请选择职业类别</option>
<?
$menu=$db->query(" select * from test where lb='0' ");
while($get_FirstCategory=$db->fetch_array($menu))
{
$show_FirstCategory .= "<option value=$get_FirstCategory[id]> $get_FirstCategory[name]</option>";
}
echo $show_FirstCategory;
?>
</select>
<select id="SecondCategory" onchange="gettext()" >
<option value="" selected="selected"> 请选择详细职位类别</option>
</select>
<input type="text" id="test" value="" />
</body>
</html>
var xmlHttp = false;
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e){
try{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch(E){
xmlHttp = false;
}
}
if(!xmlHttp && typeof XMLHttpRequest != 'undefined') {
xmlHttp = new XMLHttpRequest();
}
function ShowCategory()
{
var userid =document.getElementById("FirstCategory").value;
var url = "city.php" ;
var action = "userid=" +userid;
xmlHttp.open("GET" ,url+"?" +action,true);
xmlHttp.onreadystatechange=function(){
//提交请求后的状态
if (xmlHttp.readyState == 4) {
//HTTP请求的状态
if (xmlHttp.status == 200) {
//返回XML对象
l=document.getElementById("SecondCategory").options.length;
alert(document.getElementById("SecondCategory").options.length);
for (i = 0; i< l; i++){
document.getElementById("SecondCategory").options[1]=null;
}
var xmlDoc = xmlHttp.responseXML;
//解析XML
var root = xmlDoc.getElementsByTagName("row");
alert(root.length);
for (var x=0; x< root.length; x++){
var data=Array(2);
data[0]=root[x].selectSingleNode("id").text;
data[1]=root[x].selectSingleNode("name").text;
document.getElementById("SecondCategory" ).options[document.getElementById("SecondCategory" ).options.length] =new Option(data[1],data[1]);
}
} else {
alert('HTTP请求错误');
}
}
}
//发送请求
xmlHttp.send(null);
}
<?php
include('../admin/config.php');
//发送头文件
header('Content-Type:text/xml; charset=GB2312');
$userid = $_GET["userid" ];
$xml = "<?xml version='1.0' encoding='GB2312'?>
<users>" ;
$menu=$db->query(" select * from test where lb='".$userid."' ");
while($get_Category=$db->fetch_array($menu))
{
//生成XML数据
$xml.="<row>
<id>".$get_Category[lb]."</id>
<name>".$get_Category[name]."</name>
</row>";
}
//XML尾
$xml.="</users>" ;
//输出数据
print $xml;
exit();
?>