如果变量不为空,则显示内容

时间:2016-07-28 15:08:32

标签: javascript php jquery html

我想我越来越近了,但它没有用。非常感谢你看看!

这样做:在表单(index.html)上,当用户选择“include”radio时,内容会显示在表单上,​​然后在welcome.php上提交。除此之外它是隐藏的。

问题:如果没有在animalvillas1,animalvillas2&上输入任何值,我希望在提交时隐藏welcome.php上的内容。 animalvillas3独立。

请参阅index.html& welcome.php如下:

的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">
<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)

请尝试以下代码。

<div id="dvanimalvillas"  <?php echo ($_POST['animalvillas'] == "N") ? 'style="display:none;"' : '' ; ?>>
    <?php 
        $animalvillas1 = $_POST["animalvilas1"];
        $animalvillas2 = $_POST["animalvilas2"];
        $animalvillas3 = $_POST["animalvilas3"];

        if(!empty($animalvillas1)){
            echo "$".$_POST["animalvilas1"]." - Value Studio - Standard View <BR>";
        }
        if(!empty($animalvillas2)){
            echo "$".$_POST["animalvilas2"]." - Deluxe Studio - Standard View <BR>";
        }
        if(!empty($animalvillas3)){
            echo "$".$_POST["animalvilas3"]." - Deluxe Studio - Savanna View <BR>";
        }
    ?>
</div>

如果变量有值,则会打印整行。

检查确认 index.php
welcome.php