如果变量为空则隐藏内容[简单]

时间:2016-07-27 17:30:27

标签: php jquery html forms

这应该很简单,不确定为什么它不起作用。如果我删除if!empty,div hide正常工作。如果变量为空,我不希望显示该行。

[编辑:抱歉,将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>

的welcome.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>

1 个答案:

答案 0 :(得分:0)

使用jQuery ...

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