dongyuan2652 2013-12-30 11:09
浏览 308
已采纳

当标签文本为空时,如何隐藏Div?

I am trying to hide the 'div' by checking if a label is empty. But Something went wrong. Below is the code I used.

<script>
 function myfun(){
     if (document.getElementById('department').innerHTML=='') {
         document.getElementById('departmentdiv').style.display="none";
     }
 }
</script>
<body onload="myfun()">
  <div id="departmentdiv" class="row-fluid">
    <label>DEPARTMENT:</label>
    <label id="department"><?php if (!empty($_POST['department']))echo $_POST['department'];?></label>
  </div>
</body>

I am getting the POST[department] value from the a different page and assigned it to the label in this page. I want to show the div only if the value is not null. But, the problem is label doesn't show at all on this page even if its not null. Does anybody can help me out

  • 写回答

5条回答 默认 最新

  • duanou2016 2013-12-30 11:43
    关注

    As you can see in this JSFiddle, it does work. The problem must be with the PHP code. You don't need to test with empty(), just output the variable. If there might be whitespace in the department, trim it before outputting

    <div id="departmentdiv" class="row-fluid">
        <label>DEPARTMENT:</label>
        <label id="department"><?= trim($_POST['department']); ?></label>
    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?