2012-04-05 50 views
0

你好我還是新的阿賈克斯我想在不同的地方顯示2個數據。ajax顯示2個數據請求onclick

這裏的代碼

<li onclick="showPost(this.value);" value="*digit*" >lala</li> 

的JavaScript

<script> 
if (window.XMLHttpRequest) 
     {// code for IE7+, Firefox, Chrome, Opera, Safari 
      xmlhttp=new XMLHttpRequest(); 
     } 
     else 
     {// code for IE6, IE5 
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
     } 

     function showPost(hal) 
     { 
      xmlhttp.onreadystatechange=function() 
      { 
       if (xmlhttp.readyState==4 && xmlhttp.status==200) 
       { 
        document.getElementById("gallekanan").innerHTML=xmlhttp.responseText; 
       } 
      }  
      xmlhttp.open("GET","../wp-content/themes/koolfort/example.php?pal="+hal,true); 
      xmlhttp.send(); 
      showJudul(hal); 
     } 

     function showJudul(hal) 
     { 
      xmlhttp.onreadystatechange=function() 
      { 
       if (xmlhttp.readyState==4 && xmlhttp.status==200) 
       { 
        document.getElementById("eventjudul").innerHTML=xmlhttp.responseText; 
       } 
      }  
      xmlhttp.open("GET","../wp-content/themes/koolfort/example1.php?pal="+hal,true); 
      xmlhttp.send(); 
     } 
</script> 

當我運行的代碼只是showJudul運行和showPost被中止。

+0

首先,不要使用全局變量,使用'var'。其次,我在想... =)) – 2012-04-05 08:47:19

回答

0

移動showJudul(hal);在document.getElementById(「gallekanan」)之後。innerHTML = xmlhttp.responseText;

+0

好的。這是有效的,但showJudul沒有迴應,但該參數有價值 – 2012-04-05 08:39:50

+0

你應該檢查你的example1.php爲什麼它沒有返回任何響應。 – MACMAN 2012-04-05 09:14:30

0

儘量使每個功能的單獨控制,而不是要求他們彼此一個內..即..它作爲

<li onclick="showPost(this.value); showJudul(this.value);" value="*digit*" >lala</li> 

<script> 

     function showPost(hal) 
     { 
if (window.XMLHttpRequest) 
     {// code for IE7+, Firefox, Chrome, Opera, Safari 
      xmlhttp=new XMLHttpRequest(); 
     } 
     else 
     {// code for IE6, IE5 
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
     } 
      xmlhttp.onreadystatechange=function() 
      { 
       if (xmlhttp.readyState==4 && xmlhttp.status==200) 
       { 
        document.getElementById("gallekanan").innerHTML=xmlhttp.responseText; 
       } 
      }  
      xmlhttp.open("GET","../wp-content/themes/koolfort/example.php?pal="+hal,true); 
      xmlhttp.send(); 

     } 

     function showJudul(hal) 
     { 
if (window.XMLHttpRequest) 
     {// code for IE7+, Firefox, Chrome, Opera, Safari 
      xmlhttp=new XMLHttpRequest(); 
     } 
     else 
     {// code for IE6, IE5 
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
     } 

      xmlhttp.onreadystatechange=function() 
      { 
       if (xmlhttp.readyState==4 && xmlhttp.status==200) 
       { 
        document.getElementById("eventjudul").innerHTML=xmlhttp.responseText; 
       } 
      }  
      xmlhttp.open("GET","../wp-content/themes/koolfort/example1.php?pal="+hal,true); 
      xmlhttp.send(); 
     } 
</script> 

也可以嘗試使用try {}趕上{ }如果你嘗試初始化xmlhttp - 如果沒有這樣的對象可用,函數可能會報錯...

+0

他們是正確的迴應。身份證事件judul顯示正確的迴應,但身份證gallekanan顯示反應showjudul不showpost反應。 – 2012-04-05 08:58:31