[color=red]点击后提示缺少对象什么原因[/color]<br> var XMLHttpReq;<br> var url=null;<br> var type=null;<br> //创建XMLHttpRequest对象<br><br> function createXMLHttpRequest() {<br> if(window.XMLHttpRequest) { //Mozilla 浏览器<br> XMLHttpReq = new XMLHttpRequest();<br> }<br> else if (window.ActiveXObject) { // IE浏览器<br> try {<br> XMLHttpReq = new ActiveXObject("Msxml2.XMLHTTP");<br> } catch (e) {<br> try {<br> XMLHttpReq = new ActiveXObject("Microsoft.XMLHTTP");<br> } catch (e) {}<br> }<br> }<br> }<br> //发送请求函数<br> function sendRequest() {</p> <pre><code> createXMLHttpRequest(); var url = "/GetnameInfo"; XMLHttpReq.open("GET", url, true); XMLHttpReq.onreadystatechange = getYg_name;//指定响应函数 XMLHttpReq.send(null); // 发送请求 } // 处理返回信息函数 function getYg_name() { if (XMLHttpReq.readyState == 4) { // 判断对象状态 if (XMLHttpReq.status == 200) { // 信息已经成功返回,开始处理信息 var count = XMLHttpReq.responseXML.getElementsByTagName("count")[0].firstChild.nodeValue; for(var i=0;i<count;i++) { var name = XMLHttpReq.responseXML.getElementsByTagName("name")[i].firstChild.nodeValue; var ygid=XMLHttpReq.responseXML.getElementsByTagName("ygid")[i].firstChild.nodeValue; var op = new Option(name[i].childNodes[i].nodeValue,ygid[i].childNodes[0].nodeValue); selectObj.options.add(op); } } } } else { //页面不正常 window.alert("您所请求的页面有异常。"); } } } function nameinfo() { this.options[selectedIndex].text } </script> <body > </code></pre> <table bgcolor="#F1E1FF" width="100%" height="5%"> <tr> <td> <center> <font size="12" color="RED">员工基本信息</font> </center> </td> </tr> </table> <form name="form1"> <table> <tr> <td bgcolor="#ACD6FF"> <font size="12">部门</font> </td> <td id='tdname'> <font size="6">姓 名</font> <select name="sbox" id="selectid" style="width: 150px" onchange="nameinfo()" /> <input type="text" id="tx1" value="" name="tx1" /> </td> </tr> <tr> <td id="td1"> <br> <input type="button" id="but1" value="开发部" onclick="sendRequest();" style="width: 120px; height: 30px; background-Color: #9cc5f8"> <br> <input type="button" id="but2" value="人事部" style="width: 120px; height: 30px; background-Color: #9cc5f8"> <br> <input type="button" id="but3" value="财务部" style="width: 120px; height: 30px; background-Color: #9cc5f8"> <br> <input type="button" id="but4" value="业务部" style="width: 120px; height: 30px; background-Color: #9cc5f8"> </td> <td></td> </tr> </table> </form> <p></body></p>
if else嵌套有问题。已修改。
var XMLHttpReq; var url=null; var type=null; //创建XMLHttpRequest对象 function createXMLHttpRequest() { if(window.XMLHttpRequest) { //Mozilla 浏览器 XMLHttpReq = new XMLHttpRequest(); } else if (window.ActiveXObject) { // IE浏览器 try { XMLHttpReq = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { XMLHttpReq = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {} } } } //发送请求函数 function sendRequest() { createXMLHttpRequest(); var url = "/GetnameInfo"; XMLHttpReq.open("GET", url, true); XMLHttpReq.onreadystatechange = getYg_name;//指定响应函数 XMLHttpReq.send(null); // 发送请求 } // 处理返回信息函数 function getYg_name(){ if (XMLHttpReq.readyState == 4) { // 判断对象状态 if (XMLHttpReq.status == 200) { // 信息已经成功返回,开始处理信息 var count = XMLHttpReq.responseXML.getElementsByTagName("count")[0].firstChild.nodeValue; for(var i=0;i<count;i++){ var name = XMLHttpReq.responseXML.getElementsByTagName("name")[i].firstChild.nodeValue; var ygid=XMLHttpReq.responseXML.getElementsByTagName("ygid")[i].firstChild.nodeValue; var op = new Option(name[i].childNodes[i].nodeValue,ygid[i].childNodes[0].nodeValue); selectObj.options.add(op); } } }else { //页面不正常 window.alert("您所请求的页面有异常。"); } } function nameinfo(){ this.options[selectedIndex].text = "some test"; }员工基本信息 |
部门 | 姓 名 |
你应该把js放在最上面,不然可能你点击的时候,页面还没完全生成
最简单的方法,使用alert('a');一步一步的测试,这中找脚本的问题,最能锻炼人了,就这点活都不想做,那哪儿成啊,如果你要想从一个菜鸟变成一个菜鸟,你就等着别人帮你找吧。。。
修改createXMLHttpRequest() 函数
[code="java"]function createXMLHttpRequest()
{
try{
xhr=new XMLHttpRequest();
}catch(trymicrosoft)
{
try{
xhr=new ActiveXObject("Msxml2.XMLHTTP");
}catch(othermicrosoft)
{
try{
request=new ActiveXObject("Microsoft.XMLHTTP");
}
catch(failed)
{
request=false;
}
}
}
if(!xhr)
{
alert("err Happend!");
return null;
}
return xhr;
} [/code]