document.write()在创建XMLHttpRequest对象时不起作用

document.write() not working when XMLHttpRequest object is created

本文关键字:XMLHttpRequest 对象 不起作用 创建 write document      更新时间:2023-09-26
function state()
{
var x=document.getElementById("count").value;
document.write(x);
 if(Window.XMLHttpRequest)
{
   xmlhttp=new XMLHttpRequest();
}
else
{
   xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
  if(xmlhttp.readystate==4 && xmlhttp.status==200)
  {
   document.getElementById("response").innerHTML=xmlhttp.responseText;
  }
  xmlhttp.open("GET","getdata.php?val=".+x,"true");
  xmlhttp.send();
}
}

这是我第一次使用ajax....我想将'x'中的值发送到getdata.php...........函数状态()触发时,我从下拉菜单中选择一个值,然后单击按钮.....document.write()正在工作,如果xmlHttpRequest对象没有创建…我怎么知道如果值在字符串x被传递给getdata.php…请回答…

你不应该做document.write()。你可以把这个值赋给其他html标签,比如span之类的。

<span id="countSpan"></span>

然后你可以给这个span赋值

var x=$("#count").val();
$("#countSpan").html(x);

编辑你也可以使用jquery ajax。当您从服务器获得成功响应时,将触发成功事件。

function state() {
    var x = $("#count").val();
    $("#countSpan").html(x);
    $.ajax({
        type: "GET",
        url: "getdata.php?val=" + x,
        success: function (data) {
            $("#response").html(data)
        }
    });
}