无法识别 select 选项菜单的默认值

Default value to select option menu is not recognized

下面是用于简单 html 并且有效的代码:

<select id="name" class="state">
    <option value="Brad" selected="selected">
        Brad
    </option>

    <option value="Carol">
        Carol
    </option>

    <option value="Derrick">
        Derrick
    </option>
</select> 

我可以使用另一种方法来为我的 select 选项标签设置默认值吗?

在我的 jsp 文件中:

</html:select>
<html:option value="0">--Select--</html:option>
                    <html:option value="Brad" selected="selected">Brad</html:option>
                    <html:option value="Carol">Carol</html:option>
                </html:select>

<option value="Brad" selected="selected"> Brad </option> 的工作原理很简单 html。在单击下拉箭头之前会显示 Brad。我的问题是,当我在我的 jsp 中使用它时,它说 "The attribute is not recognized <html:option value="Brad" selected="selected">Brad</html:option>"

如果您使用 JSP,那么 html-select-dropdown 有一个 tag。使用 <form:select> 标签你可以做这样的事情 -

<form:select path="country">
   <form:option value="NONE" label="--- Select ---"/>
   <form:options items="${yourList}" />
</form:select>

要了解更多关于如何使用 jsp select 标签 深入挖掘这个 link.

并从这个 answer 获得帮助来分配默认值。

更新
这就是您可以在 JSP

中包含 JSTL 的方法
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>

<html>
    <head>
        <title></title>
    </head>
    <body>

    </body>
</html>

在这里你看到prefix="form",你可以随意命名prefix。您可以找到更多详细信息 here.

="selected" 部分不是必需的。只需 <option selected> 即可。

<select id="name" class="state">
<option selected value="Brad">
    Brad
</option>

<option value="Carol">
    Carol
</option>

<option value="Derrick">
    Derrick
</option>
</select> 

在本例中 "Brad" 的值将作为字符串传递,除非选择了另一个选项。