将responseText与另一个字符串进行比较

时间:2014-05-02 16:33:46

标签: javascript ajax jsp response responsetext

我的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'或其他内容。只有在javascript函数中执行才能声明'text is something else'。如果声明永远不会被证明。请帮忙

1 个答案:

答案 0 :(得分:2)

这是这个工作的小提琴。你的代码格式对我来说有点混乱,抱歉。我带了几个自由。如果您有问题,请告诉我!

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

Fiddle

StackOverflow about '===' operator

相关问题