php 为 HTML 表单下拉列表指定默认选定项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3608359/
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-25 10:24:39  来源:igfitidea点击:

Specify a default selected item for HTML form drop-downs

phphtmlformsdrop-down-menuselectedindex

提问by TheFlash

Typically when you need to select an item by default, you do:

通常,当您需要默认选择一个项目时,您会执行以下操作:

<select>
  <option value="1">                 Volvo  </option>
  <option value="2" selected="true"> Saab  </option>
  <option value="3">                 Mercedes  </option>
  <option value="4">                 Audi  </option>
</select>

Is it possible to get something like this?

有可能得到这样的东西吗?

<select selectedValue="2">
  <option value="1">  Volvo  </option>
  <option value="2">  Saab  </option>
  <option value="3">  Mercedes  </option>
  <option value="4">  Audi  </option>
</select>

It works out easier in PHP since you only have to soft-code one value, instead of handling the selected attribute on any possible <option/>.

在 PHP 中使用起来更容易,因为您只需对一个值进行软编码,而不用在任何可能的<option/>.

采纳答案by dmitko

Put JavaScript after the declaration of the listbox and set the selected index there:

将 JavaScript 放在列表框的声明之后,并在那里设置选定的索引:

<script>
document.getElementById('listBoxId').selectedIndex=<?php echo $INDEX ?>;
</script>

Something like this.

像这样的东西。

回答by roryf

There is no attribute like that on the <select>element. Assuming your <option>output is in a loop, I don't see how it makes a huge difference:

<select>元素上没有这样的属性。假设您的<option>输出处于循环中,我看不出它是如何产生巨大差异的:

$selected = "2";
foreach($values as $key => $val) {
    echo "<option value=\"" . $key . "\"" . ($key == $selected ? " selected=\"selected\">" : ">") . $val . "</option>";
}

(my PHP is a little rusty, that may not be 100% correct)

(我的 PHP 有点生疏,可能不是 100% 正确)

回答by corroded

No, but you can add your default value to your id like

不,但您可以将默认值添加到您的 id 中,例如

<select id="default-value-2">

then in your options, you have

然后在你的选择中,你有

<option value="2" <?php echo is_this_the_default_value ? selected='true' : '' ?>

or something to that effect(forgive me i forgot my php syntax, but i hope you get the point).

或类似的东西(请原谅我忘记了我的 php 语法,但我希望你明白这一点)。

Anyway, that is a dirty fix also, so i suggest just adding a method to check for the default selected tag and printing it selected="selected" when it is the default. You can just call it once if you loop through your select options

无论如何,这也是一个肮脏的修复,所以我建议只添加一个方法来检查默认选择的标签,并在默认时打印它 selected="selected" 。如果您循环选择选项,您可以只调用一次

回答by Serendipity

You can do it like this:

你可以这样做:

$value = 1;

<select>
<?php if($value==1) echo "<option selected='true'> Volvo </option>";
else echo "<option> Volvo </option>"; ?>
<?php if($value==2) echo "<option selected='true'> Saab </option>";
else echo "<option> Saab </option>"; ?>
</select>

回答by acSlater

I found myself googling this to see if there was a better way to do it.

我发现自己在谷歌上搜索这个,看看是否有更好的方法来做到这一点。

The best and cleanest answer is from @roryf, however if you are not looping through your data I thought it would be a lot cleaner to wrap it up in a function:

最好和最干净的答案来自@roryf,但是如果你不循环遍历你的数据,我认为将它包装在一个函数中会更清晰:

function set_selected($desired_value, $new_value)
{
    if($desired_value==$new_value)
    {
        echo ' selected="selected"';
    }
}

Then you would use it like this:

然后你会像这样使用它:

<?php $selected_value = 2; ?>

<select>
    <option value="1"<?php set_selected('1', $selected_value); ?>> Volvo  </option>
    <option value="2"<?php set_selected('2', $selected_value); ?>> Saab  </option>
    <option value="3"<?php set_selected('3', $selected_value); ?>> Mercedes  </option>
    <option value="4"<?php set_selected('4', $selected_value); ?>> Audi  </option>
</select>

This would set Saab as selected :)

这会将 Saab 设置为选中 :)