为啥 这个 onchange事件不起作用(急求)
为什么 这个 onchange事件不起作用(急求)
<%@ page language="java" pageEncoding="GBK"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script language="javascript">
var xmlHttpReq ;
function createXmlHttpReq(){
if(window.XMLHttpRequest){
xmlHtttpReq = new XMLHttpRequest();
}else{
xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
}
}
function checkUser(){
alert('测试');
var select = document.getElementById("city") ;
var index = select.selectedIndex;
var userName = select.options[index].text;
if(userName==""){
return false ;
}
createXmlHttpReq();
xmlHttpReq.onreadystatechange = handle ;
var url = ="http://localhost:8080/Test/UserServlet?name="+userName;
xmlHttpReq.open("get",url,true);
xmlHttpReq.send(null);
}
function removeElement(){
var element = document.getElementById("result");
element.options.length=0 ;
}
function create(city){
var city1 = new Array();
city1 = city.split(",");
var element_res = document.getElementById("result");
for(i = 0;i<city1.length;i++){
element_res.options.add(new Option(city1[1]);
}
}
function handle(){
alert("测试");
if(xmlHttpReq.readyState==4){
if(xmlHttpReq.status==200){
var res = xmlHttpReq.responseText;
removeElement();
create(res);
}
}
}
}
</script>
</head>
<body>
<select id="province" onChange="handle()">
<option value="-1" selected>请选择省份</option>
<option value="1">湖南</option>
<option value="2">广东</option>
</select>
<select id="result">
<option value="0">请选择城市</option>
</select>
</body>
</html>
------解决方案--------------------
1 xmlHtttpReq 拼错了
2 createXmlHttpReq()放到外面
3 element_res.options.add(new Option(city1[1]); 少了)
4 handle函数多了}
5 url加个时间戳
var url ="/Test/UserServlet?name=" + escape(userName) + "&" + escape(new Date());
<%@ page language="java" pageEncoding="GBK"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script language="javascript">
var xmlHttpReq ;
function createXmlHttpReq(){
if(window.XMLHttpRequest){
xmlHtttpReq = new XMLHttpRequest();
}else{
xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
}
}
function checkUser(){
alert('测试');
var select = document.getElementById("city") ;
var index = select.selectedIndex;
var userName = select.options[index].text;
if(userName==""){
return false ;
}
createXmlHttpReq();
xmlHttpReq.onreadystatechange = handle ;
var url = ="http://localhost:8080/Test/UserServlet?name="+userName;
xmlHttpReq.open("get",url,true);
xmlHttpReq.send(null);
}
function removeElement(){
var element = document.getElementById("result");
element.options.length=0 ;
}
function create(city){
var city1 = new Array();
city1 = city.split(",");
var element_res = document.getElementById("result");
for(i = 0;i<city1.length;i++){
element_res.options.add(new Option(city1[1]);
}
}
function handle(){
alert("测试");
if(xmlHttpReq.readyState==4){
if(xmlHttpReq.status==200){
var res = xmlHttpReq.responseText;
removeElement();
create(res);
}
}
}
}
</script>
</head>
<body>
<select id="province" onChange="handle()">
<option value="-1" selected>请选择省份</option>
<option value="1">湖南</option>
<option value="2">广东</option>
</select>
<select id="result">
<option value="0">请选择城市</option>
</select>
</body>
</html>
------解决方案--------------------
1 xmlHtttpReq 拼错了
2 createXmlHttpReq()放到外面
3 element_res.options.add(new Option(city1[1]); 少了)
4 handle函数多了}
5 url加个时间戳
var url ="/Test/UserServlet?name=" + escape(userName) + "&" + escape(new Date());
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script language="javascript">
var xmlHttpReq ;
function createXmlHttpReq(){
if(window.XMLHttpRequest){
xmlHttpReq = new XMLHttpRequest();
}else{
xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
}
}
createXmlHttpReq();
function checkUser(){
alert('测试');
var select = document.getElementById("city") ;
var index = select.selectedIndex;
var userName = select.options[index].text;
if(userName==""){
return false ;
}
xmlHttpReq.onreadystatechange = handle ;
var url ="/Test/UserServlet?name=" + escape(userName) + "&" + escape(new Date());
xmlHttpReq.open("get",url,true);
xmlHttpReq.send(null);
}
function removeElement(){
var element = document.getElementById("result");
element.options.length=0 ;
}
function create(city){
var city1 = new Array();
city1 = city.split(",");
var element_res = document.getElementById("result");
for(i = 0;i<city1.length;i++){
element_res.options.add(new Option(city1[1]));
}
}
function handle(){
alert("测试");
if(xmlHttpReq.readyState==4){
if(xmlHttpReq.status==200){
var res = xmlHttpReq.responseText;
removeElement();
create(res);
}
}
}
</script>
</head>
<body>
<select id="province" onChange="handle()">
<option value="-1" selected>请选择省份</option>
<option value="1">湖南</option>
<option value="2">广东</option>
</select>
<select id="result">
<option value="0">请选择城市</option>
</select>
</body>
</html>