2017-03-28 87 views
0

我在調用show_Message函數期待一個警告框,但onreadystatechange的不working.The其他警報框工作正常的onreadystatechange沒有得到所謂的

這裏是我的js函數

function send_Message(){ 
    var msg=document.getElementById("msg").value; 
if(msg.length===0||msg===""){ 
    alert("please enter some message"); 
    return; 
} 

var sender=document.getElementById("username").value; 
var sendto=document.getElementById("chat_id").options[document.getElementById("chat_id").selectedIndex].value; 
alert(sender+" "+sendto); 
var xhttp=new XMLHttpRequest(); 
xhttp.onreadystatechange=function(){ 
    alert('hello'); 
    if(xhttp.readyState==4 && xhttp.status==200){ 
     document.getElementById("chat_logs").innerHTML=xhttp.responseText; 
    } 
    xhttp.open('GET','send_messages.php?sender='+sender+'sendto='+sendto+'message='+msg,true); 
    xhttp.send(null); 
} 
} 
+0

你需要調用xhttp.open( 'GET',發件人+ + '的sendto =' + SENDTO + '消息=' +味精,真正); xhttp.send(null);你的onreadystatechange函數之外。 – Raj

+0

你怎麼調用'send_message'函數? – aeid

+0

並且你需要你的打開併發送到onreadstatechange – aeid

回答

0

xhttp.open('GET','send_messages.php?sender='+sender+'sendto='+sendto+'message='+msg,true); xhttp.send(null);

應該在onreadystatechange之外

將其更改爲

function send_Message(){ 
    var msg=document.getElementById("msg").value; 
if(msg.length===0||msg===""){ 
    alert("please enter some message"); 
    return; 
} 

var sender=document.getElementById("username").value; 
var sendto=document.getElementById("chat_id").options[document.getElementById("chat_id").selectedIndex].value; 
alert(sender+" "+sendto); 
var xhttp=new XMLHttpRequest(); 
xhttp.onreadystatechange=function(){ 
    alert('hello'); 
    if(xhttp.readyState==4 && xhttp.status==200){ 
     document.getElementById("chat_logs").innerHTML=xhttp.responseText; 
    } 
} 
xhttp.open('GET','send_messages.php?sender='+sender+'sendto='+sendto+'message='+msg,true); 
    xhttp.send(null); 
} 
-1

這可能是因爲你有你的公開和發送內部onstatechange功能..

function send_Message(){ 
    var msg = document.getElementById("msg").value; 
    if(msg.length === 0 || msg === ""){ 
    alert("please enter some message"); 
    return; 
    } 

    var sender=document.getElementById("username").value; 
    var sendto=document.getElementById("chat_id").options[document.getElementById("chat_id").selectedIndex].value; 
    alert(sender+" "+sendto); 

    var xhttp=new XMLHttpRequest(); 
    xhttp.onreadystatechange=function(){ 
    alert('hello'); 
    if(xhttp.readyState==4 && xhttp.status==200){ 
     document.getElementById("chat_logs").innerHTML=xhttp.responseText; 
    } 
    } 

    xhttp.open('GET','send_messages.php?sender='+sender+'sendto='+sendto+'message='+msg,true); 
    xhttp.send(null); 
} 
0

你已經做了閉幕錯誤。 xhttp.onreadystatechange = function(){

上面的函數應該在if語句後關閉。

檢查糾正下面的代碼: '?send_messages.php發件人='

function send_Message(){ 
    var msg=document.getElementById("msg").value; 
if(msg.length===0||msg===""){ 
    alert("please enter some message"); 
    return; 
} 

var sender=document.getElementById("username").value; 
var sendto=document.getElementById("chat_id").options[document.getElementById("chat_id").selectedIndex].value; 
alert(sender+" "+sendto); 
var xhttp=new XMLHttpRequest(); 
xhttp.onreadystatechange=function(){ 
    alert('hello'); 
    if(xhttp.readyState==4 && xhttp.status==200){ 
     document.getElementById("chat_logs").innerHTML=xhttp.responseText; 
    } 
}; 
    xhttp.open('GET','send_messages.php?sender='+sender+'sendto='+sendto+'message='+msg,true); 
    xhttp.send(null); 

}