我的网站上有一个精选下拉列表(www.irishbonus.comule.com/en/)。但是,当我从下拉列表中选择一个选项并按提交时,我希望该选项保持选中状态。 以下是选择的代码:
<form action="" method="post">
<strong> Select Subject:</strong>
<select name="formSubject" class="dropdown">
<option value=">>>">">>>"</option>
<option value="Accounting">Accounting</option>
<option value="Agricultural Science">Agricultural Science</option>
<option value="Agricultural Economics">Agricultural Economics</option>
<option value="Applied Mathematics">Applied Mathematics</option>
<option value="Arabic">Arabic</option>
<option value="Art (jc only)">Art (jc only)</option>
<option value="Biology">Biology</option>
<option value="Business (jc only)">Business (jc only)</option>
<option value="Business Studies">Business Studies</option>
<option value="Chemistry">Chemistry</option>
<option value="Civic (jc only)">Civic (jc only)</option>
<option value="Classical Studies">Classical Studies</option>
<option value="Construction Studies">Construction Studies</option>
//more options
<option value="Typewriting (jc only)">Typewriting (jc only)</option>
</select>
<table width="300px">
<tr>
<td valign="top">
<strong>Insert Mark:</strong>
<input type="text" name="formMark" maxlength="2" size="4" value="<?=$mark;?>"/>
</td>
</tr>
</table>
<input type="submit" />
</form>
答案 0 :(得分:0)
您需要使用服务器端语言。您似乎已经在使用PHP,所以只需添加一些PHP代码即可。最好的解决方案是将选项存储在数组中,然后迭代数组,输出<option>
标记,并在值与提交的值匹配时添加selected
属性。
<select name="formSubject" class="dropdown">
<?php
$options = ['Accounting', 'Agricultural Science', '...'];
$selected = isset($_POST['formSubject']) ? $_POST['formSubject'] : '';
foreach($options as $option) {
echo '<option value="'.$option.'"'.($selected == $option ? ' selected' : '').'>'.$option.'</option>';
}
?>
</select>
答案 1 :(得分:0)
他忘记了数组(),这就是解析错误的原因
这里:
<select name="formSubject" class="dropdown">
<?php
$options = array('Accounting', 'Agricultural Science', '...');
$selected = isset($_POST['formSubject']) ? $_POST['formSubject'] : '';
foreach($options as $option) {
echo '<option value="'.$option.'"'.($selected == $option ? ' selected' : '').'>'.$option.'</option>';
}
?>
</select>
答案 2 :(得分:0)
while ($row = $result->fetch_assoc()) {
$selected = "";
if(isset($_GET['spec'])) {
if($_GET['spec'] == $row['id']) {
$selected = "selected='selected'";
}
}
print "<option ". $selected ." value='". $row['id'] ."'>". $row['ime']. "</option>";
}
$result->free();