简体   繁体   中英

if condition is not working in javascript ajax response

In alert value is showing as initial but condition of if(checkoption == 'Initial') is return always false......... actual code is this..

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();
    }
}

Here is the corrected code.

 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM