【问题标题】:How to check if row is empty and hide a div with PHP and JQuery如何检查行是否为空并使用 PHP 和 JQuery 隐藏 div
【发布时间】:2013-12-09 10:35:24
【问题描述】:

我知道将 JavaScript 放在 PHP 中是不好的,但我正在使用 PHP 渲染 HTML,我想检查行是否为空并将这些 HTML 元素中的每一个隐藏在一个类中。我知道 Ajax,我只想在 PHP 中使用它。

PHP:

if ($row['image1'] != ''){
echo   '<script>
        $(".image1Class").each(function() {
        $(this).show();
        });
    </script>';
}

【问题讨论】:

    标签: javascript php jquery show-hide


    【解决方案1】:

    我不清楚你想做什么,但不是

    if ($row['image1'] != '')
    

    你可以使用

    if(!empty($row['image1']))
    

    【讨论】:

      【解决方案2】:

      你可以这样做:

      <script>
        $(document).ready(
          function (){
            <?php if ($row['image1'] != ''): ?>
              $(".image1Class").show();
            <?php else: ?>
              $(".image1Class").hide();
            <?php endif; ?>
          }
        );
      </script>
      

      【讨论】:

      • 你在那个类中隐藏了所有东西,我只想隐藏空的 div,这就是我使用 $(this) 的原因。
      【解决方案3】:

      希望这对您有所帮助

       $(".image1Class").each(function () { 
           if ($(this).attr("src") == "" ) 
           {
             $(this).css("display","none");  
            }
           });
      

      【讨论】:

      • 您应该在执行操作之前先使用 PHP 评估表中值的存在,我希望在将任何内容呈现给浏览器之前完成此操作。
      • 所以你的条件应该用PHP来完成,从sql中获取表状态。
      猜你喜欢
      • 2012-01-19
      • 2010-11-16
      • 1970-01-01
      • 1970-01-01
      • 2023-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多