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

Use data from JQuery .get function in an Else If statement

本文关键字:get 函数 数据 JQuery Else If 语句      更新时间:2023-09-26

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

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

当我调用一个以数据为参数的警报时,它会返回"yes",但是if语句仍然没有运行并更改images-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";
}
?>

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

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