0
<form method="post">
  <select name="select_employee" id="select_employee">
    <?php $allemp=$this->AllEmployees; ?>
    <option selected value="">Select an employee..</option>
    <?php foreach($allemp as $row){ echo "<option value=".$row[ 'Id']. ">".$row[ 'Etunimi']. " - ". $row[ 'Sukunimi']. "</option>"; } ?>
  </select>
  <input type="hidden" name="send" value="namesent">
  <input type="submit" value="submit" id="button">
</form>
<br/>
<br/>

当我提交此表单时,我停留在同一页面上,但我希望在同一时间保持先前选择的选项可见。我该怎么做?我试过:

document.getElementById('select_employee').value = "<?php echo $_GET['select_employee'];?>";

但它没有工作..

4

2 回答 2

1

这里:

 foreach($allemp as $row){
     echo "<option " . (isset($_POST['select_employee']) && $_POST['select_employee'] == $row['Id'] ? ' selected ' : '') . " value=".$row['Id'].">".$row['Etunimi']." - ".$row['Sukunimi']."</option>";
}
于 2013-03-05T12:50:33.217 回答
0

试试这个 :

 $selected = ($row['Id'] == $_REQUEST['select_employee'])?'selected="selected"':'';
 echo '<option '.$selected.' value="'.$row['Id'].'">'.$row['Etunimi'].' - '. $row['Sukunimi'].'</option>';
  1. 在上面添加了一行echo options ($selected = ($row['Id'] == $_REQUEST['select_employee'])?'selected="selected"':'';)

  2. 添加'.$selected.'在侧面选项中。

您的代码变为:复制粘贴下面的代码而不是您的代码

<form method="post">
  <select name="select_employee" id="select_employee">
    <?php $allemp=$this->AllEmployees; ?>
    <option selected value="">Select an employee..</option>
    <?php foreach($allemp as $row){ 
      $selected = ($row['Id'] == $_REQUEST['select_employee'])?'selected="selected"':'';
     echo '<option '.$selected.' value="'.$row['Id'].'">'.$row['Etunimi'].' - '. $row['Sukunimi'].'</option>';
 } ?>
  </select>
  <input type="hidden" name="send" value="namesent">
  <input type="submit" value="submit" id="button">
</form>
<br/>
<br/>
于 2013-03-05T12:51:17.110 回答