伙计我有一个下拉列表,当我按下按钮时,我想自动标记所选行!到目前为止,我的代码如下所示,但我无法使用'
"
等等。
echo "<select id='form' name=form style='width:auto' class='form-control'>
<optgroup label='form_name'>";
foreach($results as $result)
{
$ID = $result->ID;
$name = $result->name;
$address = $result->address;
echo '<option value=$ID '.
**if(isset($_GET['form']) && $_GET['form'] == $ID ){ .' selected="selected" '. } else { .''. }**
.'>'.$ID. ". " .$name. " [" .$address.']</option>';
}
echo "</optgroup>
</select>;
提前致谢!!!
答案 0 :(得分:1)
您必须使用ternary运算符。 三元运算符是一个简单的&#34;如果那么其他&#34;只有一行。
echo '<select id="form" name="form" style="width:auto" class="form-control">';
echo '<optgroup label="form_name">';
foreach ($results as $result) {
$id = $result->ID;
$name = $result->name;
$address = $result->address;
echo '<option value="' . $id . '" ' . (isset($_GET['form']) && $_GET['form'] == $id ? 'selected="selected"' : '') . '>' . $id . '. ' . $name . ' [' . $address . ']</option>';
}
echo '</optgroup>';
echo '</select>';
答案 1 :(得分:0)
您可以这样重写代码:
echo "<select id='form' name=form style='width:auto' class='form-control'>
<optgroup label='form_name'>";
foreach($results as $result)
{
$ID = $result->ID;
$name = $result->name;
$address = $result->address;
$selected = ( isset($_GET['form']) && $_GET['form'] == $ID ) ? ' selected="selected" ' : '';
echo '<option value=$ID '
. $selected
.'>'.$ID. ". " .$name. " [" .$address.']</option>';
}
echo " </optgroup>
</select>;