开发者

jQuery IF statement not working

Im performin开发者_C百科g an ajax query to check the name of a car in a mysql database, if a car is found it will return "Car name unavailable", otherwise "Car name available". This text is put into a div with an id of "checkname".

All this runs fine, but when I try to hide the add button if the car name is unavailable it fails to do so and I dont know why :/

function check_name(){

 $.ajax({
  type: "POST",
  url: "http://localhost/Framework/library/php_files/check_car_name.php",
  data:  "carName=" + document.getElementById("carName").value,
  success: function(html){
   $("#checkname").html(html);
  }
 });

 var currentHtml = $("#checkname").html();
 var compareString = "Car name unavailable";

 if (currentHtml==compareString) {
  $("#submit").hide();
 } else {
  $("#submit").show();
 }

} 


Any code that relies on the response from the AJAX request, must be called inside a callback to the request.

function check_name() {
    $.ajax({
        type: "POST",
        url: "http://localhost/Framework/library/php_files/check_car_name.php",
        data: "carName=" + document.getElementById("carName").value,
        success: function (html) {
            $("#checkname").html(html);

             // I placed your code here instead.
             // Of course you wouldn't need to set and then get the HTML,
             //    since you could just do a direct comparison.
            var currentHtml = $("#checkname").html();
            var compareString = "Car name unavailable";
            if (currentHtml == compareString) {
                $("#submit").hide();
            } else {
                $("#submit").show();
            }
        }
    });
}

The reason is that by default, an AJAX request is asynchronous, which means that the code that comes after the request will execute immediately instead of waiting for the response to return.

Another possible issue when comparing HTML to keep in mind is white space. If you're doing a string comparison, it must be exactly the same, so if there's whitespace, you'll need to trim it first. You can use jQuery.trim()(docs) to do this.


You have to put the code inside of you AJAX requests success callback, otherwise it will be called before the AJAX call has completed. Putting it inside the success callback means that the code containing the IF statement will only run after the AJAX call has completed. Try:

function check_name(){

 $.ajax({
  type: "POST",
  url: "http://localhost/Framework/library/php_files/check_car_name.php",
  data:  "carName=" + document.getElementById("carName").value,
  success: function(html){
   $("#checkname").html(html);

var currentHtml = $("#checkname").html();
 var compareString = "Car name unavailable";

 if (currentHtml==compareString) {
  $("#submit").hide();
 } else {
  $("#submit").show();
 }
  }
 });

} 


You could probably solve the problem doing what patrick dw suggested (it's likely that the ajax call has not been completed yet (i.e. div content wasn't updated) and the check returns false), but since string comparison can really bring to many errors (like string not matching because of newlines, trailing spaces, case sensitiveness, etc...) I would suggest you use another comparison method.

For example you could add a class using .addClass() if the car is found, and then checking if that div has the "found" class using .hasClass()


I use .post(). The third argument of post() is the returned data from the file where the data was posted.

I pass to validate.php the inputed email address, validate.php checks it and if it is valid, it returns 1.

$('a.post').click(function() {
$.post('validate.php',{email : $("#email-field").val()}, 

function(data){
if(data==1)     
{  
//do something if the email is valid
} else { 
//do other thing
});

});

Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜