【问题标题】:When no entries in database img tag still displays empty box当数据库 img 标签中没有条目时仍显示空框
【发布时间】:2016-06-02 00:43:09
【问题描述】:

我在编写脚本时遇到了问题。该脚本显示存储在数据库中的图像。但是,当数据库中没有图像时,它将显示一个带有损坏图像图标的空框。有没有办法阻止这种情况?

这是代码:

<script  type="text/javascript" >
  //retrieves json encoded array
  $.getJSON("retrieveSymbol.php", function(data){
    $.each(data, function(i, field){          //loops through array
      $("#bookmarkedSymbols").append(
        //sets field as image source so that the path can be displayed as an image
        "<img src='" + field[1] + "'" + "id='symbol' />"

      );
    });
  });
  </script>

【问题讨论】:

  • 您是否从函数retrieveSymbol.php 以json_encode() 的格式打印或回显?如果不是,你应该这样做。

标签: javascript php html json loops


【解决方案1】:

您需要获取json 数据长度并对其进行检查:-

<script  type="text/javascript" >
  //retrieves json encoded array
  $.getJSON("retrieveSymbol.php", function(data){
       if(data.length >0){
            $.each(data, function(i, field){ //loops through array
              $("#bookmarkedSymbols").append("<img src='" + field[1] + "'" + "id='symbol' />");
            });
        }
    });
</script>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-22
    • 2021-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-27
    • 2015-09-30
    相关资源
    最近更新 更多