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

Javascript 如果变量不为空,则显示内容,javascript,php,jquery,html,Javascript,Php,Jquery,Html,我想我越来越近了,但它不起作用。非常感谢您的关注 这是有效的:在表单(index.html)上,当用户选择“include”单选时,内容显示在表单上,然后在提交后显示在welcome.php上。这两个都是隐藏的,否则 问题是:如果animalvilllas1、animalvilllas2和animalvilllas3没有独立输入值,我也会这样做,并在提交时隐藏welcome.php上的内容 请参见下面的index.html和welcome.php: index.html <head>

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

这是有效的:在表单(index.html)上,当用户选择“include”单选时,内容显示在表单上,然后在提交后显示在welcome.php上。这两个都是隐藏的,否则

问题是:如果animalvilllas1、animalvilllas2和animalvilllas3没有独立输入值,我也会这样做,并在提交时隐藏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>

$(函数(){
$(“输入[name='animalvillas']”)。单击(函数(){
如果($(“#chkYes1”)。是(“:选中”)){
$(“#dvanimalvillas”).show();
}否则{
$(“#dvanimalvillas”).hide();
}
});
});
雅博洛奇别墅酒店
包括
排除

$-Value Studio-标准视图(停车场视图)
$-豪华工作室-标准视图(水/池)
$-豪华工作室-热带草原景观
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>
$-Value Studio-标准视图
$-豪华工作室-标准视图
$-豪华工作室-热带草原景观
尝试以下代码

<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>

如果变量有值,则打印整行,否则不打印

检查它


是的,很抱歉,我把这个片段拉出来显示在这里,忘了带if(isset($\u POST['yourvar'))…谢谢Lemmy。我用过这个,但如果值为空,行仍会显示。$-value Studio-标准视图
愚蠢的问题,你有没有回显$animalvillas1/2/3?显示的值是什么?好的,我已经编辑了这个,并按照你说的那样尝试了,现在它工作正常。你能验证一下吗。我已经复制/粘贴/上传/尝试了你编辑的代码e次和我提交的任何东西,welcome.php都会出现空白。我已经放置了一个指向我编写的文件的dropbox链接。您可以检查自己。检查它是否有帮助或让我知道。Hemant,谢谢,但这已经删除了主要部分的单选功能是的,我没有包括单选按钮,但当您应用它们时,that将起作用,这是转到下一页并显示/隐藏行的代码