Unable to compare two Strings in javascript

馋奶兔 提交于 2019-12-12 03:17:34

问题


I am trying two compare two strings in JavaScript. But I guess there is some problem while comparing. It doesn't show the results.

if(ajaxRequest.readyState == 4){

    var msg = ajaxRequest.responseText;
    var fld = document.getElementById("prtCnt");
    if(msg == "false") {
        var msg = "This User Name is already taken !!!!";
        fld.className = "bp_invalid";
        //   fld.style.color=green;
        fld.innerHTML=msg;
    }

Can any body tell me where the problem is? Thanks.


回答1:


You might want to check if there's any space before or after the "false" string that you return from the server. You can do it easily with this:

alert('"' + msg + '"');

If there is extra space, you can just do:

msg = msg.trim();

and then do your if statement




回答2:


Ensure that there is no white space around the word "false" with something like:

if( msg.match(/\s*false\s*/i) )


来源:https://stackoverflow.com/questions/9543633/unable-to-compare-two-strings-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!