javascript - if 条件在 javascript ajax 响应中不起作用

标签 javascript ajax

警报值显示为初始值,但 if(checkoption == 'Initial') 的条件始终返回 false...... 实际代码是这样的..

function changeItem(id,item,option) {

    var xhttp; 
    xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
            if(this.responseText=="Initial"){
            $("#Initial").load("Item-<%=cha_id%>.html #Initial", function()
            {
                document.getElementById("Initial").style.display = 'block';
                document.getElementById("Final").style.display = 'none';
            });
        }
        if(this.responseText=="Final"){
            $("#Final").load("Item-<%=cha_id%>.html #Final", function()
            {
                document.getElementById("Initial").style.display = 'none';
                document.getElementById("Final").style.display = 'block';
            }); 
        } 
    }
    };
    xhttp.open("GET", "<%=landingpage_basepath %>/ChangeItemValue111?info="+id+"&item="+item+"&option="+option, true);
    xhttp.send();
    }
}

最佳答案

这是更正后的代码。

 xhttp.onreadystatechange = function() {
   if (this.readyState == 4 && this.status == 200) {
      var checkoption=this.responseText;

      alert(checkoption);

   if(checkoption == 'Initial') {  alert(this.responseText);  }
   else if(checkoption=="Final"){  alert(this.responseText);  }
   else {  alert("Hello"); }
 }
};


xhttp.open("GET", "<%=landingpage_basepath %>/ChangeItemValue111?info="+id+"&item="+item+"&option="+option, true);
xhttp.send();

关于javascript - if 条件在 javascript ajax 响应中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45055853/

相关文章:

javascript - Firefox Add-on SDK 获取选项卡 ID

javascript - 从字符串数组中查找单词,仅整个单词(带有希伯来字符)

javascript - 否则条件不工作

jquery - 在ajax加载的内容中预加载图像

php - 使用 ajax json 动态响应设置 webos 列表选择器选项

javascript - 从回调函数内部使用 "pure"Node.js 重定向

javascript - Firebase 数据库 startAt() 字符串参数长度限制?

javascript - 使用 vuejs 和 axios 显示来自 JSON API 的数据

javascript - 使用 jquery load 将文本插入到 div 中

ajax - Django - 自定义装饰器仅允许 ajax 请求