selected
最后更新于:2022-04-01 11:04:04
> selected指令专门用来解决选择框的自动选中问题, 可省去多个if判断
~~~
<select class="form-control" php-init="$p = isset($_GET['p']) ? $_GET['p'] : 1">
<option php-for="$i = 1; $i <= 50; $i++" php-selected="$p == $i" value="{$i}">第{$i}页</option>
</select>
~~~
解析后:
~~~
<?php $p = isset($_GET['p']) ? $_GET['p'] : 1; ?>
<select class="form-control">
<?php
for ($i = 1; $i <= 50; $i++) {
if ($p == $i) { ?>
<option selected="selected" value="<?php echo $i; ?>">第<?php echo $i; ?>页</option>
<?php } else { ?>
<option value="<?php echo $i; ?>">第<?php echo $i; ?>页</option>
<?php }
}
?>
</select>
~~~