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

标签 javascript php jquery html

我想我越来越接近了,但它不起作用。非常感谢您的关注!

这有效:在表单 (index.html) 上,当用户选择“包含”单选按钮时,内容会显示在表单上,​​然后在提交后显示在 welcome.php 上。否则,它对两者都是隐藏的。

问题:如果没有在 animalvillas1、animalvillas2 和 animalvillas3 上独立输入值,我想做同样的事情并在提交时隐藏 welcome.php 上的内容。

请参阅下面的 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>

欢迎.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>

最佳答案

尝试以下代码。

<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

关于javascript - 如果变量不为空则显示内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38640249/

相关文章:

php - 在 Woocommerce 中以编程方式设置或更新产品运输类别

php - 使用 MySql php 计算循环字段

javascript - 在字段模糊/焦点丢失时使用 jQuery 更新表单字段的错误样式

javascript - 正则表达式 - 排除括号和带特殊键的括号

javascript - 结合显示/隐藏切换与单选按钮和复选框

javascript - 在发送更新数据之前需要提交两次表单

javascript - nodejs cli 返回 [函数 : values] for []. 值

javascript - AngularJS 输入字段在提交后显示 [object Object]

php - mysql 查询(连接)和添加子查询

javascript - JS 图片加载性能