Javascript IF execution error
The following is a part of my code :
$.post开发者_如何学JAVA("checkbuddy.php",function(data){
if(data!="a"){
$("#Layer15").css({ visibility: 'visible'});
$("#Layer10").css({ visibility: 'visible'});
$("#Layer10").html(data);
}
});
The condition if(data!="a")
doesn't seem to be working, as checkbuddy.php returns "a"
. And what happens is that the HTML of Layer10 changes to "a"
, and thus the text shown is "a"
. This shouldn't be happening should it?
Whitespace may well be the problem. Use jQuery's .trim()
function:
$.post("checkbuddy.php",function(data){
if($.trim(data) !="a"){
$("#Layer15").show();
$("#Layer10").show();
$("#Layer10").html(data);
}
});
Extra space after the closing ?>
tag or before the <?php
?
Check the whitespace, as said in the other answers, but it can also be the MIME type. If checkbuddy.php is not returning a text/html or text/plain MIME type and is instead text/xml, jQuery might be converting it into an object of some sort, which would mess up the comparison.
精彩评论