Html 使用请求参数在下拉列表中选择一个选项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7646488/
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
selecting an option in a dropdown list using request parameters
提问by DaTaBomB
I'm kind of new to this so please bear with me. I have a value from the db which needs to set the currently selected value in a dropdown .this is passed to the front end as a request parameter. I tried using selected="parameter" attribute with the select tag but doesnt seem to work. I also tried value="", no result. Any help is appreciated. Thanks
我对此很陌生,所以请耐心等待。我有一个来自数据库的值,它需要在下拉列表中设置当前选定的值。这作为请求参数传递给前端。我尝试将 selected="parameter" 属性与 select 标记一起使用,但似乎不起作用。我也试过 value="",没有结果。任何帮助表示赞赏。谢谢
回答by Xavi López
If you want to serve the jsp with a selected value in the <select>
, put the selected
attribute in the corresponding option
.
如果要服务于JSP与在选择的值<select>
,把selected
属性的相应option
。
<select id="dropdown">
<option value="1">option 1</option>
<option value="2" selected>option 2</option>
<option value="3">option 3</option>
</select>
Use the JSTL tag <c:if>
to test for equality between the request parameter and the current value. For instance, with a request parameter named selectValue
:
使用 JSTL 标记<c:if>
来测试请求参数和当前值之间的相等性。例如,使用名为 的请求参数selectValue
:
<option
<c:if test='${param.selectValue == currentOption}'> selected </c:if>
>
</option
Take a look at the following question for details: Selected item populating in Select tag using JSTL?
有关详细信息,请查看以下问题:使用 JSTL 在 Select 标记中填充所选项目?
To elaborate on the matter, in case you're building your options dinamically (<c:forEach>
), while looping the options, output selected
in the <option>
when the current <option>
's value is equal to the request parameter (code untested):
为了阐述这个问题,如果你dinamically(构建选项<c:forEach>
),而循环播放的选项,输出selected
在<option>
当电流<option>
的值等于请求参数(未测试的代码):
<select id="dropdown">
<c:forEach var="item" items="${list}">
<option value="<c:out value='${item}' />"
<c:if test="${param.selectValue == item})"> selected </c:if> >
<c:out value="${item}" />
</option>
</c:forEach>
</select>
In case the options are static, you could add something like this in each <option>
(code untested):
如果选项是静态的,您可以在每个选项中添加如下内容<option>
(代码未经测试):
<option value="1"
<c:if test="${param.selectValue == 1})"> selected </c:if> >
option 1
</option>
<option value="2"
<c:if test="${param.selectValue == 2})"> selected </c:if> >
option 2
</option>
If you want to set the selected value through javascript, use for instance
如果要通过javascript设置选定的值,例如使用
document.getElementById("dropdown").value = <c:out value='${param.selectValue}'/>
回答by BalusC
To the point, you need to set the selected
attribute onlywhenever the submitted value matches the selected value. Cleanest would be to use the conditional operator ?:
in EL for this as using JSTL <c:if>
would end up in clumsy and hard-to-read code.
为了这一点,你需要设置selected
属性只有每当提交值的选定值相匹配。最干净的方法是?:
在 EL 中使用条件运算符,因为使用 JSTL<c:if>
会导致代码笨拙且难以阅读。
Here's an example:
下面是一个例子:
<select name="foo">
<option value="1" ${param.foo == '1' ? 'selected' : ''}>One</option>
<option value="2" ${param.foo == '2' ? 'selected' : ''}>Two</option>
<option value="3" ${param.foo == '3' ? 'selected' : ''}>Three</option>
</select>
Please note that the foo
in ${param.foo}
must exactly match name of <select name="foo">
. Please also note that the ${param.foo}
value must exactly match the value of <option value>
. The usage of id
as suggested in other answer is irrelevant here. It's only relevant for the client side. IDs aren't used as request parameter names or values at all.
请注意foo
in${param.foo}
必须与 的名称完全匹配 <select name="foo">
。另请注意,该${param.foo}
值必须与 的值完全匹配<option value>
。id
其他答案中建议的使用在这里无关紧要。它只与客户端相关。ID 根本不用作请求参数名称或值。
回答by Ralph
You can solve this with javascript and jquery. After the page was rendered you simply update the select box and set the value provided in a param. See the following jsp/jquery example:
你可以用 javascript 和 jquery 来解决这个问题。页面呈现后,您只需更新选择框并设置参数中提供的值。请参阅以下 jsp/jquery 示例:
<select id="dropdown">
<option value="1">option 1</option>
<option value="2">option 2</option>
<option value="3">option 3</option>
</select>
<script type="text/javascript">
$(document).ready(function() {
// set select options with jquery and EL
$("#dropdown").val("${param.foo}");
});
</script>
In a JSF/Facelets page you can use this EL syntax (replace ${} with #{})
在 JSF/Facelets 页面中,您可以使用此 EL 语法(将 ${} 替换为 #{})
$("#dropdown").val("#{param.foo}");