Ajax Call仅在第一次运行

时间:2011-02-25 17:51:28

标签: javascript ajax javascript-events

我的javascript / ajax代码第一次运行,但之后没有。再次调用该函数时,GetAttribute元素为null。我尝试使用createElement和AppendChild,但它做同样的事情。如果我不需要getAttribute它会工作正常,但我无法使用getAttribute方法的函数。任何帮助将不胜感激。

function ajaxFunction(Picked) { 
var getdate = new Date();
 if(xmlhttp) { 
    var Pic1 = document.getElementById("Pic1").getAttribute("name");
    var Pic2 = document.getElementById("Pic2").getAttribute("name");
    if (Picked === Pic1 ){
    var Chosen = Pic1;
    var NotChosen = Pic2;
}
else {
    var Chosen = Pic2;
    var NotChosen = Pic1;
}
    xmlhttp.open("POST","choice.php",true);
    xmlhttp.onreadystatechange  = handleServerResponse;
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlhttp.send("Chosen=" + Chosen + "&NotChosen=" + NotChosen );
    }
 }

function handleServerResponse() {
  if (xmlhttp.readyState == 4) {
  if(xmlhttp.status == 200) {
       var response = xmlhttp.responseText;
       response = response.split("|");
       document.getElementById('Pic1').innerHTML = response[0];//New Pic
       document.getElementById('Pic2').innerHTML = response[1];        
  }
  else {
    alert("Error. Please try again");
  }
 }
}

1 个答案:

答案 0 :(得分:1)

if更改为

if (Picked === Pic1)

通过撰写if (Picked = Pic1 ),您 Picked分配给Pic1