如果为null则为活动,然后在PHP中隐藏div

时间:2014-04-30 15:23:05

标签: javascript php jquery html

我想我必须为此使用Jquery。

我有一个打印按钮,我只想显示我的数组中是否包含数据。

这是我试图完成的简化版本

HTML

<?include 'anotherpage.php'?>
<button class='printButton' name='printButton'>Print</button>

PHP

$data = array();
if($statement == true){
 array_push($data, 'some value', 'another value');
}
if (empty($data)){
  echo "No trace currently found";
  **<<I want to set a hidden class to the button here somehow>>**
}

非常感谢任何正确方向的帮助。我还在努力掌握整个JQuery的事情。

3 个答案:

答案 0 :(得分:1)

为什么不包含按钮如果有$ statement == true?

另一种方法是设置一个隐藏按钮的jQuery / JavaScript函数。 然后,如果没有按钮,则打印一个调用该函数的html部分。

这样的事情:

的jQuery

hidePrintButton = function() 
{
    $('.printbutton').hide();
}

PHP

if (empty($data)){
  echo "No trace currently found";
  echo '<script>';
  echo 'hidePrintButton();';
  echo '</script>';
}

答案 1 :(得分:0)

将按钮放在PHP中的if语句中怎么样?

<强> PHP

$data = array();
if($statement == true){
 array_push($data, 'some value', 'another value');
 echo "<button class='printButton' name='printButton'>Print</button>";
}
if (empty($data)){
  echo "No trace currently found";
  **<<I want to set a hidden class to the button here somehow>>**
}

<强> HTML

<?include 'anotherpage.php'?>

答案 2 :(得分:0)

如果按钮已在页面上全部呈现,并且假设只有一个按钮具有类printButton,则使用jquery可以执行以下操作:

if(empty($ data)){       回声“目前没有找到痕迹”;       echo“$('。printButton')。hide();”;    }

但这不是一个好方法,因为你混合服务器行为和客户端行为,这将导致代码难以维护和调试。 您最好重写代码以仅使用php,如果不需要则不首先打印按钮,或者如果以后需要使用类隐藏它。 使用类隐藏按钮:

$hideClass = "";
if (empty($data)){
    $hideClass = "hideButton";    
}
echo "<button class='printButton $hideClass' name='printButton'>Print</button>";

这样,如果您以后需要使用jquery显示按钮,可以调用:

$('.printButton').removeClass('hideButton');