选定的 Html 选择选项不起作用

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

Html select option selected does not work

htmlcombobox

提问by Suzan Cioc

How to set default selected element in combobox? Neither of the following worked for me:

如何在组合框中设置默认选定元素?以下都不适合我:

Version 3:

版本 3:

<select id="parent">
    <option value='null'>(Root)</option>
    <option selected='yes' value='/'>Main</option>
</select>

Version 2:

版本 2:

<select id="parent">
    <option value='null'>(Root)</option>
    <option selected='true' value='/'>Main</option>
</select>

Version 1:

版本 1:

<select id="parent">
    <option value='null'>(Root)</option>
    <option selected value='/'>Main</option>
</select>

In all cases first option is selected on page, not that one which is marked selected.

在所有情况下,第一个选项在页面上被选中,而不是被标记为选中的那个。

SOME OTHER DETAILS

其他一些细节

(1)

(1)

Page source beginning follows:

页面源码开头如下:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        ...

(2)

(2)

Browser is Firefox 13.0.1

浏览器是 Firefox 13.0.1

采纳答案by Matzi

Add selectedto the option you want to be selected by default. Check the following example taken from here.

添加selected到您要默认选择的选项。检查取自此处的以下示例。

<!-- The second value will be selected initially -->
<select name="choice">
  <option value="first">First Value</option>
  <option value="second" selected>Second Value</option>
  <option value="third">Third Value</option>
</select>

回答by mercurial

I'm just going to throw this out there because it has driven me crazy on more than one occasion:

我只想把它扔出去,因为它不止一次让我发疯:

When you refresh (F5) a page, Firefox preserves the selected values of any <select> elements on the page.

当您刷新 (F5) 页面时,Firefox 会保留页面上任何 <select> 元素的选定值。

So if you're like me and write the select menu, load the page to make sure it works, then add the "selected" attribute, and refresh the page to make sure the selected one appears ... it won't (in Firefox, anyway).

因此,如果您像我一样编写选择菜单,加载页面以确保其正常工作,然后添加“selected”属性,并刷新页面以确保所选的出现......它不会(在无论如何,Firefox)。

Reloading the page entirely by clicking in the Address field and pressing Enter does honor the "selected" attribute.

通过单击“地址”字段并按 Enter 完全重新加载页面确实符合“selected”属性。

回答by user25794

Maybe the autocomplete form attribute is throwing you out.

也许自动完成表单属性让你失望。

Try adding autocomplete="off" to the form tag.

尝试将 autocomplete="off" 添加到表单标记中。

This way even when you refresh the page in FF the selected attribute is respected.

这样,即使您在 FF 中刷新页面,也会尊重所选属性。

GL

GL