checked

最后更新于:2022-04-01 11:04:06

> checked指令专门用来解决复选框和单选的自动选中问题, 可省去多个if判断 ~~~ <div class="form-control" php-init="$all_hobby = ['html','css','js','php', 'mysql', 'linux']" php-init="$user_hobby = ['linux','css','js','php']"> 技能: <label class="checkbox-inline" php-repeat="$all_hobby as $hobby"> <input type="checkbox" name="hobby" value="{$hobby}" php-checked="in_array($hobby, $user_hobby)" /> {$hobby} </label> </div> <br /> <div class="form-control" php-init="$user_sex = 1"> 性别: <label class="radio-inline"> <input type="radio" name="sex" value="0" php-checked="$user_sex === 0" /> 女 </label> <label class="radio-inline"> <input type="radio" name="sex" value="1" php-checked="$user_sex === 1" /> 男 </label> </div> ~~~ 解析后: ~~~ <?php $all_hobby = ['html','css','js','php', 'mysql', 'linux']; $user_hobby = ['linux','css','js','php']; ?> <div class="form-control"> 技能: <?php foreach ($all_hobby as $hobby) { ?> <label class="checkbox-inline"> <?php if (in_array($hobby, $user_hobby)) { ?> <input type="checkbox" name="hobby" value="<?php echo $hobby; ?>" checked="checked" /> <?php } else { ?> <input type="checkbox" name="hobby" value="<?php echo $hobby; ?>" /> <?php } echo $hobby; ?> </label> <?php } ?> </div> <br /> <?php $user_sex = 1; ?> <div class="form-control"> 性别: <label class="radio-inline"> <?php if ($user_sex === 0) { ?> <input type="radio" name="sex" value="0" checked="checked" /> <?php } else { ?> <input type="radio" name="sex" value="0" /> <?php } ?> 女 </label> <label class="radio-inline"> <?php if ($user_sex === 1) { ?> <input type="radio" name="sex" value="1" checked="checked" /> <?php } else { ?> <input type="radio" name="sex" value="1" /> <?php } ?> 男 </label> </div> ~~~
';