php HTML 定义激活的选择选项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8370746/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:33:09  来源:igfitidea点击:

HTML define what selection option is active

phphtmldrop-down-menu

提问by JustinPGriffen

I have a select box with the following (it has been shortened):

我有一个带有以下内容的选择框(已缩短):

<select id="viewSelector" name="when" style="width:92%;">
    <option value="USA">USA</option>
    <option value="Australia">Australia</option>
    <option value="Germany">Germany</option>
</select>

If the user logs into the control panel and wants to change his country, he gets presented with this form. My problem is that everytime USA is the default and I can not change this depending on the users country. For example, the user lives in Australia. He wants to change his country to USA and goes to this page. I want the country that is displayed to be Australia. Let me know if this makes sense.

如果用户登录控制面板并想要更改他的国家/地区,则会显示此表单。我的问题是,每次美国都是默认值,我无法根据用户所在的国家/地区进行更改。例如,用户居住在澳大利亚。他想将他的国家/地区更改为美国并转到此页面。我希望显示的国家是澳大利亚。让我知道这是否有意义。

回答by Bj?rn Kaiser

To preselect a value, just add the selected attribute to the desired option.

要预选一个值,只需将所选属性添加到所需选项即可。

<select id="viewSelector" name="when" style="width:92%;">
    <option value="USA">USA</option>
    <option value="Australia" selected="selected">Australia</option>
    <option value="Germany">Germany</option>
</select>

This will preselect Australia for example.

例如,这将预选澳大利亚。

回答by Oded

You need to add a selectedattribute to the option you want to select, as described in the spec.

您需要为selected要选择的选项添加一个属性,如规范中所述。

<select id="viewSelector" name="when"">
    <option value="USA">USA</option>
    <option value="Australia" selected="selected">Australia</option>
    <option value="Germany">Germany</option>
</select>

In your script, you need to emit this attribute for whatever default you want to display.

在您的脚本中,您需要为要显示的任何默认值发出此属性。

回答by Bailey Parker

Use selectedon the <option>tag that names the user's current country:

selected<option>命名用户当前国家的标签上使用:

<option value="Australia" selected="selected">Australia</option>

So in PHP:

所以在 PHP 中:

<select>
<?php
$countries = array('USA', 'Australia', 'Germany');
$current_country = 'Australia';

foreach($countries as $country) {
    if($country == $current_country) {
        echo '<option selected="selected">'.$country.'</option>';
    } else {
        echo '<option>'.$country.'</option>';
    }
}
?>
</select>

回答by Tyilo

Use the selectedattribute of the optiontag.

使用标签的selected属性option

<select id="viewSelector" name="when" style="width:92%;">
    <option value="USA">USA</option>
    <option value="Australia">Australia</option>
    <option selected value="Germany">Germany</option>
</select>

回答by Francis Avila

The html looks like:

html 看起来像:

<option selected value="Australia">Australia</option>

In the php loop that builds this html, compare the current option name with the user's current country (retrieved from a database or where-ever). When the two match, add selected

在构建此 html 的 php 循环中,将当前选项名称与用户的当前国家/地区(从数据库或任何地方检索)进行比较。当两者匹配时,添加selected

回答by Carlos Chaveztoro

A bit shorter:

短一点:

foreach($paises as $key=>$value)
{
    if($key == "PE"){$activo = ' selected="selected" '; }else{$activo = "";}    
    echo "<option $activo value=\"$key\">$value</option>";
}