在Else If语句中使用来自JQuery .get函数的数据

时间:2015-12-10 22:23:23

标签: javascript php jquery sql

我正在使用JQuery运行一个简单的.get语句:

$.get("check.php", function(data)
{
    if (data == "yes")
    {
        $('#thisimg').attr('src' ,'yes.jpg');
    }
    else
    {
        $('#thisimg').attr('src' ,'no.jpg');
    }
});

当我使用数据作为参数调用警报时,它返回"是",但if语句仍未运行并更改图像src。

这是我的check.php文件:

<?php
//connect to db here
//run sql select statement to check whether or not the value is 0 or 1
//value is saved into variable $selected, and the value is 1
if ($selected == 1)
{
      echo "yes";
}
else                    
{
    echo "no";
}
?>

1 个答案:

答案 0 :(得分:3)

通常,返回的数据中会有一个不可见的字符。如果您.trim data它应该有效:

if (data.trim() == "yes")
{
    $('#thisimg').attr('src' ,'yes.jpg');
}
else
{
    $('#thisimg').attr('src' ,'no.jpg');
}