如果在字段

时间:2016-07-28 21:13:49

标签: javascript php jquery html

我是新手,试图弄明白。非常感谢你看看!

这有效:当用户选择'include'radio时,在表单(index.html)上,内容会显示在表单上,​​然后在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 
        $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>

1 个答案:

答案 0 :(得分:1)

welcome.php中,您可以添加以下内容:

$hide = $_POST['animalvillas'] === "N" || ( 
            empty($_POST["animalvilas1"]) 
            && empty($_POST["animalvilas2"]) 
            && empty($_POST["animalvilas3"]));

<div id="dvanimalvillas"  <?php echo $hide ? 'style="display:none;"' : '' ; ?>>
    <?php ....  ?>
</div>

如果$_POST['animalvillas'] === "N"或所有输入都为空,这将隐藏内容!

我希望这会有所帮助。