【问题标题】:Hide content if variable is empty [simple]如果变量为空,则隐藏内容[简单]
【发布时间】:2016-07-27 17:30:27
【问题描述】:

这应该很容易,不知道为什么它不起作用。如果我删除 if !empty,则 div 隐藏将正常工作。我不希望显示变量是否为空的行。

[编辑:抱歉,没有列出 index.html]

index.html

<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
    $(function () {
        $("input[name='animalvillas']").click(function () {
            if ($("#chkYes1").is(":checked")) {
                $("#dvanimalvillas").show();
            } else {
                $("#dvanimalvillas").hide();
            }
        });
    });
    </script>
</head>
<form name="create" action="welcome.php" method="post" onsubmit="return validateForm();">
<span><strong>Lodge Villas - Jambo</strong></span><BR />
<label for="chkYes1">
    <input type="radio" id="chkYes1" name="animalvillas" value="Y" />
    Include
</label>
<label for="chkNo1">
    <input type="radio" id="chkNo1" name="animalvillas" value="N" checked />
    Exclude
</label>
<hr />
<div id="dvanimalvillas" style="display: none">
$<input type="text" name="animalvillas1" size="3" /> - Value Studio - Standard View (Parking View) <br>
$<input type="text" name="animalvillas2" size="3" /> - Deluxe Studio - Standard View (Water / Pool) <br>
$<input type="text" name="animalvillas3" size="3" /> - Deluxe Studio - Savanna View <br>
</div>
<input type="submit" />
</form>

欢迎.php

    <div id="dvanimalvillas" <?php echo ($_POST['animalvillas'] == "N") ? 'style="display:none;"' : '' ; ?>>
<?php if (!empty($animalvillas1)) { ?>$<?php echo $_POST["animalvillas1"]; ?> - Value Studio - Standard View <BR><?php } ?>
<?php if (!empty($animalvillas2)) { ?>$<?php echo $_POST["animalvillas2"]; ?> - Deluxe Studio - Standard View <BR><?php } ?>
<?php if (!empty($animalvillas3)) { ?>$<?php echo $_POST["animalvillas3"]; ?> - Deluxe Studio - Savanna View <BR><?php } ?>
</div>

【问题讨论】:

  • 所以($x == 'N') || ($x == '')?
  • 变量和$_POST键之间的关系是什么,例如$animalvillas1$_POST["animalvillas1"]?
  • 感谢您的回复,我编辑了 OP 以包含 index.html

标签: php jquery html forms


【解决方案1】:

使用 jQuery...

If (yourVariable <= 0){
    $('dvanimalvillas').hide ();
}

【讨论】:

  • 感谢您的回复,我编辑了 OP 以包含 index.html。这将阐明我想要做什么。
猜你喜欢
  • 2012-01-08
  • 1970-01-01
  • 1970-01-01
  • 2013-04-27
  • 2014-02-27
  • 1970-01-01
  • 2018-12-31
  • 2022-01-16
  • 2021-11-20
相关资源
最近更新 更多