2011-08-02 29 views
1

我希望在一個下拉的onchange事件中同時調用3個ajax函數。在onchange事件中調用2個ajax函數

我試着調用第二個函數的第一個函數的if (xmlhttp1.readyState==4 && xmlhttp1.status==200)和第二個函數的第二個函數if (xmlhttp1.readyState==4 && xmlhttp1.status==200)的第二個函數。

但是隻調用第一個函數,即使第二個函數沒有被調用。 任何其他方式來實現它?

下面的功能是:

function list1(ob){ 

    var id= ob; 
    var xmlhttp1; 

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

    xmlhttp1.onreadystatechange = function(){ 
     if (xmlhttp1.readyState==4 && xmlhttp1.status==200){ 
      alert("ajax1"); 
      list2(id); 
     } 
    } 

    xmlhttp1.open("POST","/ajax/listSites.php",true); 
    xmlhttp1.setRequestHeader("Content-type", 
           "application/x-www-form-urlencoded"); 
    xmlhttp1.send("id="+id);  
} 


function list2(obb){ 

    var idd=obb; 
    var xmlhttp; 

    if (window.XMLHttpRequest){// 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){ 
      alert("ajax2"); 
      list3(idd); 
     } 
    } 

    xmlhttp.open("POST","/ajax/access_points_do_actions.php",true); 
    xmlhttp.setRequestHeader("Content-type", 
          "application/x-www-form-urlencoded"); 
    xmlhttp. 
     send("sel_system_id="+idd+ 
      "&action="+'LIST_SLAVE_FOR_ACCESS_POINT'); 

     } 

// only alert ajax1 is displayed 
+0

你可以發佈你試過的代碼,所以我們可以看到你哪裏錯了? –

+0

增加了他的代碼,我沒有去掉第三個函數,寫了2個函數 – look4php

+0

對不起,我的意思是代碼下拉也是。 –

回答

2

你可以只連鎖異步調用是這樣的:

onchange="Function1(); Function2();" 

更新:

您的代碼:

<select id="se" onchange="javascript:list1(this.value); //this is current code 

應somethi ng像:

<select id="se" onchange="Javascript: list1(this); list1(this);"> 
+0

函數應該由昏迷分開onchange =「Function1(),Function2();」 – look4php

+0

不應該將它們作爲帶有分號(;)的獨立語句執行。由於它們是異步執行的,所以第二次應該在第一次結束時執行,但仍然在等待結果。 –

+0