代码在FF下测试成功,但在IE下不成功(IE6.7.8均测试不成功)
var http_request=false;
function send_request(url){//初始化,指定处理函数,发送请求的函数
http_request=false;
//开始初始化XMLHttpRequest对象
if(window.XMLHttpRequest){//Mozilla浏览器
http_request=new XMLHttpRequest();
if(http_request.overrideMimeType){//设置MIME类别
http_request.overrideMimeType("text/xml");
}
}
else if(window.ActiveXObject){//IE浏览器
try{
http_request=new ActiveXObject("Msxml2.XMLHttp");
}catch(e){
try{
http_request=new ActiveXobject("Microsoft.XMLHttp");
}catch(e){}
}
}
if(!http_request){//异常,创建对象实例失败
window.alert("创建XMLHttp对象失败!");
return false;
}
http_request.onreadystatechange=processrequest;
//确定发送请求方式,URL,及是否同步执行下段代码
http_request.open("GET",url,true);
http_request.send(null);
}
//处理返回信息的函数
function processrequest(){
if(http_request.readyState==4){//判断对象状态
if(http_request.status==200){//信息已成功返回,开始处理信息
document.getElementById(reobj).innerHTML=http_request.responseText;
}
else{//页面不正常
alert("您所请求的页面不正常!");
}
}
}
function getclass(obj){
var pid=document.form1.select1.value;
var act="do1";
document.getElementById(obj).innerHTML="<option>loading...</option>";
send_request('doclass.php?pid='+pid+'&act='+act);
reobj=obj;
} function getclass2(obj){
var pid=document.form1.select2.value;
var act="do2";
document.getElementById(obj).innerHTML="<option>loading...</option>";
send_request('doclass.php?pid='+pid+'&act='+act);
reobj=obj;
}
<script language="javascript" src="ajaxmenu.js"></script>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>ajax2级联动菜单演示</title>
</head><body>
<form name="form1">
<select name="select1" id="class1" style="width:100;" onChange="getclass('class2');">
<option selected="selected"></option>
<option value="2">大类1</option>
<option value="4">大类2</option>
</select>
<select name="select2" id="class2" style="width:100;" onChange="getclass2('class3');">
</select>
<select name="select3" id="class3" style="width:100;">
</select>
</form>
</body>
</html>
var http_request=false;
function send_request(url){//初始化,指定处理函数,发送请求的函数
http_request=false;
//开始初始化XMLHttpRequest对象
if(window.XMLHttpRequest){//Mozilla浏览器
http_request=new XMLHttpRequest();
if(http_request.overrideMimeType){//设置MIME类别
http_request.overrideMimeType("text/xml");
}
}
else if(window.ActiveXObject){//IE浏览器
try{
http_request=new ActiveXObject("Msxml2.XMLHttp");
}catch(e){
try{
http_request=new ActiveXobject("Microsoft.XMLHttp");
}catch(e){}
}
}
if(!http_request){//异常,创建对象实例失败
window.alert("创建XMLHttp对象失败!");
return false;
}
http_request.onreadystatechange=processrequest;
//确定发送请求方式,URL,及是否同步执行下段代码
http_request.open("GET",url,true);
http_request.send(null);
}
//处理返回信息的函数
function processrequest(){
if(http_request.readyState==4){//判断对象状态
if(http_request.status==200){//信息已成功返回,开始处理信息
document.getElementById(reobj).innerHTML=http_request.responseText;
}
else{//页面不正常
alert("您所请求的页面不正常!");
}
}
}
function getclass(obj){
var pid=document.form1.select1.value;
var act="do1";
document.getElementById(obj).innerHTML="<option>loading...</option>";
send_request('doclass.php?pid='+pid+'&act='+act);
reobj=obj;
} function getclass2(obj){
var pid=document.form1.select2.value;
var act="do2";
document.getElementById(obj).innerHTML="<option>loading...</option>";
send_request('doclass.php?pid='+pid+'&act='+act);
reobj=obj;
}
<script language="javascript" src="ajaxmenu.js"></script>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>ajax2级联动菜单演示</title>
</head><body>
<form name="form1">
<select name="select1" id="class1" style="width:100;" onChange="getclass('class2');">
<option selected="selected"></option>
<option value="2">大类1</option>
<option value="4">大类2</option>
</select>
<select name="select2" id="class2" style="width:100;" onChange="getclass2('class3');">
</select>
<select name="select3" id="class3" style="width:100;">
</select>
</form>
</body>
</html>
IE下ajax需要通过服务器来测试
1. 定义全局变量 var reobj;
2. reobj=obj; 放到send_request()上面;
3. send_request参数中加一个时间戳防止缓存 "...&" + escape(new Date())
4. ie里不能用innerHTML更新select, 可以返回ajax可以连<select>标签一起返回,
用outerHTML更新 document.getElementById(reobj).outerHTML=http_request.responseText;
var reobj;
var pid=document.form1.select1.value;
var act="do1";
document.getElementById(obj).innerHTML="<option>loading...</option>";
reobj=obj;
send_request('doclass.php?pid='+pid+'&act='+act);
}
var id=document.form.areaid.value;
var act="do1";
document.getElementById(obj).outerHTML=http_request.responseText;
reobj=obj;
send_request('doclass.php?pid='+id+'&act='+act);
}在IE提示document.getElementById为空或不是对象我对JS一窍不通哈,只是做PHP的。谢谢好心的hookee
document.getElementById(obj).outerHTML="<select name='select1' id='" + obj + "' style='width:100;'><option>loading... </option></select>";
var id=document.form.areaid.value;
var act="do1";
document.getElementById(obj).outerHTML="<select name='select1' id='" + obj + "' style='width:100;'><option>loading... </option></select>";
reobj=obj;
send_request('doclass.php?pid='+id+'&act='+act);
} 改成这样了,还是不行,FF下可以用,IE下查询不到值测试地址:http://119.45.64.250/cms/test 谢谢
http_request.responseText返回的是 <option>(内容)</option>IE下document.getElementById(reobj).outerHTML= //重新构造select 例如<select name='select1' id='"+obj+"' style='width:100;'><option>loading... </option></select>;
var id=document.form.areaid.value;
var act="do1";
document.getElementById(obj).outerHTML="<select name='select1' id='" + obj + "' style='width:100;'><option value='1'>大类1</option><option value='3'>大类2</option></select>";
reobj=obj;
send_request('doclass.php?pid='+id+'&act='+act); } ???
</select>
<script language="javascript">window.onload=function(){
document.getElementById('class3').innerHTML='<option>loading...</option>'}
</script>
下面是一个大概的修改
function processrequest(){
if(http_request.readyState==4){//判断对象状态
if(http_request.status==200){//信息已成功返回,开始处理信息
//=============================这里需要修改过,要使用sel.options.add的来添加option
//你可以返回一个json对象的数组的字符串表示的对象,如 [{txt:'显示的值',value:'value'},{txt:'显示的值',value:'value'}]
//然后eval生产数组对象,然后遍历数组向select中使用sel.options.add来添加option
//document.getElementById(reobj).innerHTML=http_request.responseText;var o=document.getElementById(reobj)
,arr=eval('('+http_request.responseText+')');//eval生产数组对象
o.options.length=0//要注意先清空原来的option
for(var i=0;i<arr.length;i++)sel.options.add(new Option(arr[i].txt,arr[i].value))//===========
}
else{//页面不正常
alert("您所请求的页面不正常!");
}
}
}
function getclass(obj){
var pid=document.form1.select1.value;
var act="do1";
var o=document.getElementById(obj)
o.options.length=0//===========这样设置来清空options
o.options.add(new Option('loading...'))//===========
//========document.getElementById(obj).innerHTML="<option>loading...</option>";
send_request('doclass.php?pid='+pid+'&act='+act);
reobj=obj;
} function getclass2(obj){
var pid=document.form1.select2.value;
var act="do2";
var o=document.getElementById(obj)
o.options.length=0//===========这样设置来清空options
o.options.add(new Option('loading...'))//===========
//========== document.getElementById(obj).innerHTML="<option>loading...</option>"; send_request('doclass.php?pid='+pid+'&act='+act);
reobj=obj;
}
<select name="select2" id="class2" style="width:100;" onChange="getclass2('class3');">
<option>xxxx</option>
</select>照上面4点改,ie下测试过是可以的.