另一种使用可重用性的方法(如WordPress中所用)
链接到WordPress代码是:
<select name='result[interest]' style="width:400px">
<option value='SEO' <?php selected($result['interest'], 'SEO'); ?>>SEO</option>
<option value='AUTO' <?php selected($result['interest'], 'AUTO'); ?>>Auto</option>
</select>
选定函数(selected())用于确定值是否被选择并设置所选选项。
function selected( $selected, $current = true, $echo = true ) {
return __checked_selected_helper( $selected, $current, $echo, 'selected' );
}
selected()函数接下来调用下面的辅助函数来比较并确定该值是否被选中。它返回'selected=selected'或''。
function __checked_selected_helper( $helper, $current, $echo, $type ) {
if ( (string) $helper === (string) $current ) {
$result = " $type='$type'";
} else {
$result = '';
}
if ( $echo ) {
echo $result;
}
return $result;
}
htmlspecialchars($var)
是你的好朋友。https://dev59.com/IG025IYBdhLWcg3wCxVN - edhurtig