2012-02-05 33 views
-1

它应该显示172-ajax-include.php当用户点击提交按钮。但它不起作用。我认为我做对了。无法弄清楚我做错了什么。为什么我的阿贾克斯工作

<html> 
<head> 
    <script type="text/javascript"> 
    function load() 
    { /* start function load */ 
     if (window.XMLHttpRequest) 
     { /* 1 if start */ 

     xmlhttp = new XMLHttpRequest(); 

     } /* 1 if end */ 
     else 
     { /* 1 else start */ 
     xmlhttp = new ActiveXObject('Microsoft.XMLHTTP'); 
     } /* 1 else end */ 

     xmlhttp.onreadystatechange = function() { 
     if (xmlhttp.readyState == 4 && xmlhttp.status == 200 /* the 200 makes sure that it's not empty*/) 
     { /* 2 if start */ 
      document.getElementById('adiv'),innerHTML = xmlhttp.responseText; 
     } /* 2 if end */ 

     xmlhttp.open('GET', '172-ajax-include.php', true); 
     xmlhttp.send(); 
     } 



    } /* end function load */ 
    </script> 

</head> 
<body> 

    <input type="submit" onclick="load();"> 
    <div id="adiv"></div> 
</body> 
</html> 

回答

1

open()和send()必须放在onreadystatechange处理程序之外。

xmlhttp.onreadystatechange = function() 
    { 
     if (xmlhttp.readyState == 4 && xmlhttp.status == 200) 
     { /* 2 if start */ 
      document.getElementById('adiv'),innerHTML = xmlhttp.responseText; 
     } /* 2 if end */ 
    }; 
    xmlhttp.open('GET', '172-ajax-include.php', true); 
    xmlhttp.send(); 

编辑:

更改HTML按钮

<input type"button" onclick="load()" value="Show"/> 
+0

谢谢AVD,但它仍然没有工作,我试过了。有任何想法吗? – 2012-02-05 06:57:08

+0

更改按钮类型。 adatapost 2012-02-05 07:03:54

+0

谢谢老兄。我也搞砸了最后一条陈述。我用逗号代替了句点。是!得到它的工作。 – 2012-02-05 07:13:57