【问题标题】:Use data from JQuery .get function in an Else If statement在 Else If 语句中使用来自 JQuery .get 函数的数据
【发布时间】:2016-03-16 17:37:26
【问题描述】:

我正在使用 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";
}
?>

【问题讨论】:

  • 在您的console 中记录dataconsole.log(data);.
  • 它输出“是”@brso05
  • console 中是否有任何错误?
  • 数据的类型是什么?尝试 typeof(data) ,如果没有调用 if 语句(true 块或 else 块),则一定有错误。

标签: javascript php jquery sql


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-28
    • 1970-01-01
    • 1970-01-01
    • 2018-02-28
    • 2011-09-11
    相关资源
    最近更新 更多