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"/>

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.


var field = $("#email");

              var email = $(field).val();



         }); });

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.

2014/04/25 18:34
  • servlets
  • ajax
  • javascript
  • jquery
  • 点赞
  • 收藏
  • 回答