当 value = null 时隐藏部分 HTML 表单
Hide parts of HTML form when value = null
我为学校做了一个小投票系统,当一个问题没有 4 个答案时我遇到了问题。
我希望 html 表单仅显示具有值的按钮。
Screenshot of the voting system
按钮 3 和 4 未定义。只有按钮 1 和 2 应可见且可点击。
这是我的代码:
<form role="form" class="form-inlinecy" action="VotingVoting.php" method="post">
<div class="form-group">
<input type="submit" name="a" id="a" value="<?php echo $voting->a ?>"/>
</div>
<div class="form-group">
<input type="submit" name="b" id="b" value="<?php echo $voting->b ?>"/>
</div>
<div class="form-group">
<input type="submit" name="c" id="c" value="<?php echo $voting->c ?>"/>
</div>
<div class="form-group">
<input type="submit" name="d" id="d" value="<?php echo $voting->d ?>"/>
</div>
</form>
听起来并不复杂,但我在这里找不到答案。非常感谢!
您好
蒂莫
使用PHP脚本检查
<?php if(isset($voting->a) && !empty($voting->a))
{ ?>
<div class="form-group">
<input type="submit" name="a" id="a" value="<?php echo $voting->a ?>"/>
</div>
<?php } ?>
对所有四个输入字段执行相同的操作
您有多种方法可以做到这一点,一种可能是在输入周围添加条件,如下所示:
<?php if($voting->b != ""){ ?>
<div class="form-group">
<input type="submit" name="b" id="b" value="<?php echo $voting->b ?>"/>
</div>
<?php } ?>
或者 CSS 方式:
input[value=""] { display: none; }
您可以使用 PHP 条件如下隐藏最后两个按钮,如果它们没有设置的话。
<form role="form" class="form-inlinecy" action="VotingVoting.php" method="post">
<div class="form-group">
<input type="submit" name="a" id="a" value="<?php echo $voting->a ?>"/>
</div>
<div class="form-group">
<input type="submit" name="b" id="b" value="<?php echo $voting->b ?>"/>
</div>
<?php if (isset($voting->c)) { ?>
<div class="form-group">
<input type="submit" name="c" id="c" value="<?php echo $voting->c ?>"/>
</div>
<?php } ?>
<?php if (isset($voting->d)) { ?>
<div class="form-group">
<input type="submit" name="d" id="d" value="<?php echo $voting->d ?>"/>
</div>
<?php } ?>
</form>
您可以使用 if 条件,如下所示
<?php if(isset($voting->a) && !empty($voting->a)){ ?>
<div class="form-group">
<input type="submit" name="a" id="a" value="<?php echo $voting->a ?>"/>
</div>
<?php } ?>
这应该有效:
<form role="form" class="form-inlinecy" action="VotingVoting.php" method="post">
<div class="form-group">
<?php
if($voting->a <> '') { echo '<input type="submit" name="a" id="a" value="'.$voting->a.'"/>';}
?>
</div>
<div class="form-group">
<?php
if($voting->b <> '') { echo '<input type="submit" name="b" id="b" value="'.$voting->b.'"/>';}
?>
</div>
<div class="form-group">
<?php
if($voting->c <> '') { echo '<input type="submit" name="c" id="c" value="'.$voting->c.'"/>';}
?>
</div>
<div class="form-group">
<?php
if($voting->d <> '') { echo '<input type="submit" name="d" id="d" value="'.$voting->d.'"/>';}
?>
</div>
</form>
试试这个
<form role="form" class="form-inlinecy" action="VotingVoting.php" method="post">
<div class="form-group">
<?php
if($voting->a != '') { echo '<input type="submit" name="a" id="a" value="'.$voting->a.'"/>';}
?>
</div>
<div class="form-group">
<?php
if($voting->b != '') { echo '<input type="submit" name="b" id="b" value="'.$voting->b.'"/>';}
?>
</div>
<div class="form-group">
<?php
if($voting->c != '') { echo '<input type="submit" name="c" id="c" value="'.$voting->c.'"/>';}
?>
</div>
<div class="form-group">
<?php
if($voting->d != '') { echo '<input type="submit" name="d" id="d" value="'.$voting->d.'"/>';}
?>
</div>
</form>
您的 class 结构总体上不是很好,但对于您的示例
<form role="form" class="form-inlinecy" action="VotingVoting.php" method="post">
<?php
foreach (['a','b','c','d'] as $id) {
if (empty($voting->$id)) continue;?>
<div class="form-group">
<input type="submit" name="<?php echo $id;?>" id="<?php echo $id;?>" value="<?php echo $voting->$id; ?>"/>
</div>
<?php } ?>
</form>
我为学校做了一个小投票系统,当一个问题没有 4 个答案时我遇到了问题。 我希望 html 表单仅显示具有值的按钮。 Screenshot of the voting system
按钮 3 和 4 未定义。只有按钮 1 和 2 应可见且可点击。
这是我的代码:
<form role="form" class="form-inlinecy" action="VotingVoting.php" method="post">
<div class="form-group">
<input type="submit" name="a" id="a" value="<?php echo $voting->a ?>"/>
</div>
<div class="form-group">
<input type="submit" name="b" id="b" value="<?php echo $voting->b ?>"/>
</div>
<div class="form-group">
<input type="submit" name="c" id="c" value="<?php echo $voting->c ?>"/>
</div>
<div class="form-group">
<input type="submit" name="d" id="d" value="<?php echo $voting->d ?>"/>
</div>
</form>
听起来并不复杂,但我在这里找不到答案。非常感谢!
您好 蒂莫
使用PHP脚本检查
<?php if(isset($voting->a) && !empty($voting->a))
{ ?>
<div class="form-group">
<input type="submit" name="a" id="a" value="<?php echo $voting->a ?>"/>
</div>
<?php } ?>
对所有四个输入字段执行相同的操作
您有多种方法可以做到这一点,一种可能是在输入周围添加条件,如下所示:
<?php if($voting->b != ""){ ?>
<div class="form-group">
<input type="submit" name="b" id="b" value="<?php echo $voting->b ?>"/>
</div>
<?php } ?>
或者 CSS 方式:
input[value=""] { display: none; }
您可以使用 PHP 条件如下隐藏最后两个按钮,如果它们没有设置的话。
<form role="form" class="form-inlinecy" action="VotingVoting.php" method="post">
<div class="form-group">
<input type="submit" name="a" id="a" value="<?php echo $voting->a ?>"/>
</div>
<div class="form-group">
<input type="submit" name="b" id="b" value="<?php echo $voting->b ?>"/>
</div>
<?php if (isset($voting->c)) { ?>
<div class="form-group">
<input type="submit" name="c" id="c" value="<?php echo $voting->c ?>"/>
</div>
<?php } ?>
<?php if (isset($voting->d)) { ?>
<div class="form-group">
<input type="submit" name="d" id="d" value="<?php echo $voting->d ?>"/>
</div>
<?php } ?>
</form>
您可以使用 if 条件,如下所示
<?php if(isset($voting->a) && !empty($voting->a)){ ?>
<div class="form-group">
<input type="submit" name="a" id="a" value="<?php echo $voting->a ?>"/>
</div>
<?php } ?>
这应该有效:
<form role="form" class="form-inlinecy" action="VotingVoting.php" method="post">
<div class="form-group">
<?php
if($voting->a <> '') { echo '<input type="submit" name="a" id="a" value="'.$voting->a.'"/>';}
?>
</div>
<div class="form-group">
<?php
if($voting->b <> '') { echo '<input type="submit" name="b" id="b" value="'.$voting->b.'"/>';}
?>
</div>
<div class="form-group">
<?php
if($voting->c <> '') { echo '<input type="submit" name="c" id="c" value="'.$voting->c.'"/>';}
?>
</div>
<div class="form-group">
<?php
if($voting->d <> '') { echo '<input type="submit" name="d" id="d" value="'.$voting->d.'"/>';}
?>
</div>
</form>
试试这个
<form role="form" class="form-inlinecy" action="VotingVoting.php" method="post">
<div class="form-group">
<?php
if($voting->a != '') { echo '<input type="submit" name="a" id="a" value="'.$voting->a.'"/>';}
?>
</div>
<div class="form-group">
<?php
if($voting->b != '') { echo '<input type="submit" name="b" id="b" value="'.$voting->b.'"/>';}
?>
</div>
<div class="form-group">
<?php
if($voting->c != '') { echo '<input type="submit" name="c" id="c" value="'.$voting->c.'"/>';}
?>
</div>
<div class="form-group">
<?php
if($voting->d != '') { echo '<input type="submit" name="d" id="d" value="'.$voting->d.'"/>';}
?>
</div>
</form>
您的 class 结构总体上不是很好,但对于您的示例
<form role="form" class="form-inlinecy" action="VotingVoting.php" method="post">
<?php
foreach (['a','b','c','d'] as $id) {
if (empty($voting->$id)) continue;?>
<div class="form-group">
<input type="submit" name="<?php echo $id;?>" id="<?php echo $id;?>" value="<?php echo $voting->$id; ?>"/>
</div>
<?php } ?>
</form>