我想我越来越接近了,但它不起作用。非常感谢您的关注!
这有效:在表单 (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/