JQuery中的字符串比较

I have this registration form

    <form method="post" action="Register">
        <input type="text" name="name" placeholder="Name"/><br>
        <input type="email" name="email" placeholder="Email" id="email"/><br>
        <span id="taken">This email is already taken!</span>
        <input type="password" name="password" placeholder="Password" />  <br>
        <input type="submit" value="Sumbit" id="button"/>
    </form>

And this JS code is supposed to check whether the email exists in the database or not by calling a servlet using AJAX when focus leaves the email textarea.

$(document).ready(function(){

var field = $("#email");

        $(field).focusout(function(){
              var email = $(field).val();

                $.get("Verify",{"email":email},function(data){
                 alert(data);
                    if(data=="true")
                        $("#taken").css("display","block");

                    if(data=="false")
                        $("#taken").css("display","none");


                });
         }); });

The servlet is returning the correct result (the code works until alert(data)) which is a String that's either "true" or "false", I checked that it's a String by alerting "typeof data". What I want is to display the span $("#taken") if the AJAX response (data) was equal to "true" and hide it otherwise. My problem is that the comparison is not working! I've tried using "===" instead of "==" and I have also tried converting data to boolean by using

var existed = Boolean(data);

to use to use "if(existed)" and "if(!existed)" instead of comparing strings

Nothing worked! I know the answer must be silly but I have truly spent the last two hours looking into it and nothing I have tried worked out. Please help.

查看全部
weixin_33729196
weixin_33729196
2014/04/25 18:34
  • servlets
  • ajax
  • javascript
  • jquery
  • 点赞
  • 收藏
  • 回答
    私信

1个回复