如何从<select>标记中选择一个值

时间:2016-03-18 22:46:55

标签: php html html-select

我实现了以下下拉列表: &lt; select style =&#34; margin-left:25px;&#34;&gt;      &lt; option value =&#34; null&#34;&gt; &LT; /选项&GT;      &lt; option value =&#34; single&#34;&gt; Single&lt; / option&gt;      &lt; option value =&#34; in_a_relationship&#34;&gt;在关系&lt; / option&gt;中      &lt; option value =&#34;订婚&#34;&gt;订婚&lt; /选项&gt;      &lt; option value =&#34;已婚&#34;&gt;已婚&lt; /选项&gt;      &lt; option value =&#34; open_rel&#34;&gt;在开放式关系中&lt; / option&gt;      &lt; option value =&#34;离婚&#34;&gt;离婚&lt; /选项&gt;  &LT; /选择&GT; 比如说,用户已经从下拉菜单中选择了Single。当他们返回此页面时,我希望从列表中选择单个。对于文本字段,我只需从数据库中获取值,然后将其分配给变量,然后在值中回显它。即 $ get_data2 = mysqli_query($ connect,&#34; SELECT * FROM user_bio WHERE username =&#39; $ username&#39;&#34;);         $ row3 = mysqli_fetch_assoc($ get_data2);                 $ bio = $ row3 [&#39; about_me&#39;]; 然后...... &lt; textarea name =&#34; biotextarea&#34;形式=&#34; change_details&#34;行=&#34; 4&#34; COLS =&#34; 60&#34; maxlength =&#34; 255&#34;&gt;&lt;?php echo $ bio; ?&GT; &LT; / textarea的&GT; 如何为select标签执行此操作?

3 个答案:

答案 0 :(得分:1)

在select标签上设置预选选项需要您在所选选项上放置一个选定的属性。

<select style="margin-left:25px;">
<?php
   $options = array(array("null"," "),array("single","Single"),array("in_a_relationship","In a relationship"),array("engaged","Engaged"),array("married","Married"),array("open_rel","In an open relationship"),array("divorced","Divorced"));
   foreach ($option in $options){
       if ($option[0] == $row3['status']){
           $selected = 'selected ';
       }
       else{
           $selected = '';
       }
       echo '<option '.$selected.'value="'.htmlspecialchars($option[0]).'">'.htmlspecialchars($option[1]).'</option>';
   } 
?>
 </select>

答案 1 :(得分:1)

以下是在HTML中选择Single选项的方法:

<select style="margin-left:25px;">
     <option value="null"> </option>
     <option value="single" selected>Single</option>
     <option value="in_a_relationship">In a relationship</option>
     <option value="engaged">Engaged</option>
     <option value="married">Married</option>
     <option value="open_rel">In an open relationship</option>
     <option value="divorced">Divorced</option>
 </select>

那么,如何在PHP中设置它?!

好吧,你可以像这样生成你的<select>元素:

<?php
$options = [
    'null' => ' ',
    'single' => Single',
    'in_a_relationship' => 'In a relationship',
    'engaged' => 'Engaged',
    'married' => 'Married',
    'open_rel' => 'In an open relationship',
    'divorced' => 'Divorced'
];
?>
<select style="margin-left:25px;"><?php
     foreach($options as $key => $value) {
          echo '<option value="' . $key . '"';
          if ($key === $row3['status']){
              echo ' selected';
          }
          echo '>' . $value . '</option>';
     }
?></select>

答案 2 :(得分:0)

Musa的解决方案没关系,但是如果你不想改变你的PHP太多 你可以使用Javascript完成任务。

使用jQuery或纯Javascript

function selector(id, value){
    select = document.getElementById(id);
  for(i=0;i<select.options.length;i++){
    if(select.options[i].value == value){
        select.selectedIndex = i;
    }
  }

}
//Pure Javascript
selector("status", "engaged")

//With jQuery
jQuery('#status').val('married');

选中此Snippet