比较responseText与另一个字符串

compare responseText with another string

本文关键字:字符串 另一个 responseText 比较      更新时间:2023-09-26

我的index.html文件有这样的代码:

<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script>
            function ajaxObj(str){
                var xmlhttp;
                if(window.ActiveXObject){
                    try{
                        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
                    }catch(e){
                        xmlhttp=false;
                    }
                }
                else{
                    try{
                        xmlhttp=new XMLHttpRequest();
                    }
                    catch(e){
                        xmlhttp=false;
                    }
                }
                if(!xmlhttp)
                    alert("cant create the xmlHttp object");
                else
                    //alert("objet created");
                xmlhttp.onreadystatechange=function(){
                    if(xmlhttp.readyState==4 && xmlhttp.status==200){
                        //  alert("in ready state");
                        var resp=xmlhttp.responseText;
                        document.getElementById("div1").innerHTML=resp;
                        if(resp=="pal"){               
                            document.getElementById("div2").innerHTML="text is pal";
                        }
                        else{
                            document.getElementById("div2").innerHTML="text is something else";
                        }
                    }
                }
                xmlhttp.open("GET","mainJsp.jsp?q="+str,true);
                xmlhttp.send();
            }
        </script>
    </head>
    <body>
        <table>
            <tr>
                <td>
                    <input type="text" name="userInput" onblur="ajaxObj(this.value)"/>
                </td>
            </tr>
            <tr>
                <td>
                    <div id="div1"></div>
                </td>
            </tr>
            <tr>
                <td>
                    <div id="div2"></div>
                </td>
            </tr>
        </table>
    </body>
</html>

和mainJsp.jsp的代码如下:

<%
    String textValue=request.getParameter("q");
    if(textValue.equals("pal")){
        out.println(textValue);
    }
    /*if(textValue.equals("mohit")){
        out.println(textValue);
    }*/
    else{
        out.println("else");
    }
%>

我是否在index.html的文本框中输入'pal'或其他东西。只有else语句"文本是别的东西"在javascript函数得到执行。If语句永远不会被执行。请帮助

这是一个工作原理。代码的格式对我来说有点混乱,抱歉。我做了一些改动。如果你有什么问题,请告诉我!

if(textValue === "pal"){
    alert("If");
}
else{
    alert("else");
}

小提琴

StackOverflow about '===' operator