我期待一个关于调用show_Message函数的警告框但是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 :(得分: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 :(得分:0)
你做错了。 xhttp.onreadystatechange =函数(){
上面的函数应该在你拥有的if语句之后关闭。
检查以下更正的代码:
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);
}
答案 2 :(得分:-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);
}