2011-06-08 49 views
0

我有一個使用AJAX的問題。這兩個函數通過onclick事件調用,發佈每個值將AJAX和背部數據切換爲兩個div。如果只在一個onclick中放置一個功能。它運行良好。但放2,只運行最後一個ajax2('".$data."','".$num2."');。哪裏有問題?onClick的Javascript 2函數只運行最後一個

echo "<a OnClick=\"ajax1('".$data."','".$num1."');ajax2('".$data."','".$num2."');\">" click </a>"; 

兩個js代碼,功能類似。只是功能號碼,處理頁面和ruturn div不一樣。

功能1

function ajax1(data,number) { 
     HttPRequest = false; 
     if (window.XMLHttpRequest) { 
     HttPRequest = new XMLHttpRequest(); 
     if (HttPRequest.overrideMimeType) { 
      HttPRequest.overrideMimeType('text/html'); 
     } 
     } else if (window.ActiveXObject) { 
     try { 
      HttPRequest = new ActiveXObject("Msxml2.XMLHTTP"); 
     } catch (e) { 
      try { 
       HttPRequest = new ActiveXObject("Microsoft.XMLHTTP"); 
      } catch (e) {} 
     } 
     } 
     } 
     var url = 'page1.php'; 
     var pmeters = "data=" + data + "&number=" + number; 
     HttPRequest.open('POST',url,true); 
     HttPRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
     HttPRequest.send(pmeters); 
     HttPRequest.onreadystatechange = function() 
     { 
      if(HttPRequest.readyState == 4) 
       { 
       document.getElementById("return1").innerHTML = HttPRequest.responseText;     
       } 
     } 
    } 

函數2

function ajax2(data,number) { 
     HttPRequest = false; 
     if (window.XMLHttpRequest) { 
     HttPRequest = new XMLHttpRequest(); 
     if (HttPRequest.overrideMimeType) { 
      HttPRequest.overrideMimeType('text/html'); 
     } 
     } else if (window.ActiveXObject) { 
     try { 
      HttPRequest = new ActiveXObject("Msxml2.XMLHTTP"); 
     } catch (e) { 
      try { 
       HttPRequest = new ActiveXObject("Microsoft.XMLHTTP"); 
      } catch (e) {} 
     } 
     } 
     } 
     var url = 'page2.php'; 
     var pmeters = "data=" + data + "&number=" + number; 
     HttPRequest.open('POST',url,true); 
     HttPRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
     HttPRequest.send(pmeters); 
     HttPRequest.onreadystatechange = function() 
     { 
      if(HttPRequest.readyState == 4) 
       { 
       document.getElementById("return2").innerHTML = HttPRequest.responseText;     
       } 
     } 
    } 
+0

你真的應該重構你的代碼。使**一個**功能,也接受目標元素ID和URL。並且不會用PHP回顯HTML。 – 2011-06-08 08:53:16

回答

3

變化這兩者的功能的第一行至var HttPRequest = false;。這使得httprequest對每個函數都是私有的。

相關問題