Html 使用 jstl 在现有选择标签上设置选定选项

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

Set selected option on existing select tag with jstl

htmljspjstl

提问by Faiyet

So I have a select for the grade on each subject. It's predefined and hence I don't have to store grades as a table in the database. I have a list of qualifications and I using jstl for each like this: <c:forEach items="${qualificationdetails}" var="qd">.

所以我对每个科目的成绩都有一个选择。它是预定义的,因此我不必将成绩作为表存储在数据库中。我有资格的名单,我使用JSTL每个这样的:<c:forEach items="${qualificationdetails}" var="qd">

For each item I am producing a select like this.

对于每个项目,我都在制作这样的选择。

<select class="grade" title="Grade Obtained">
   <option value="1">1</option>
   <option value="2">2</option>
   <option value="3">3</option>
   <option value="A">A</option>
   <option value="B">B</option>
   <option value="C">C</option>
   <option value="D">D</option>
   <option value="E">E</option>
</select>

Is it possible to set the selected option using my variable qd? something like

是否可以使用我的变量qd设置所选选项?就像是

<option value="${qd.grade}" selected="selecetd">${qd.grade}</option>

This will add a duplicate option to the select. I think it would a bit "clunky" to make an array with the grades and send it accross for generating the options. Any ideas ?

这将为选择添加一个重复的选项。我认为用成绩创建一个数组并发送它来生成选项会有点“笨拙”。有任何想法吗 ?

回答by BalusC

You could just let JSP render the selectedattribute conditionally.

您可以让 JSPselected有条件地呈现属性。

<select class="grade" title="Grade Obtained">
    <option value="1" ${qd.grade == '1' ? 'selected' : ''}>1</option>
    <option value="2" ${qd.grade == '2' ? 'selected' : ''}>2</option>
    <option value="3" ${qd.grade == '3' ? 'selected' : ''}>3</option>
    <option value="A" ${qd.grade == 'A' ? 'selected' : ''}>A</option>
    <option value="B" ${qd.grade == 'B' ? 'selected' : ''}>B</option>
    <option value="C" ${qd.grade == 'C' ? 'selected' : ''}>C</option>
    <option value="D" ${qd.grade == 'D' ? 'selected' : ''}>D</option>
    <option value="E" ${qd.grade == 'E' ? 'selected' : ''}>E</option>
</select>

Alternatively, you could just create a collection/array of grades and store it in the application scope so that it's available in EL so that you can loop over it using <c:forEach>. I'm not sure how that would be "clunky". You could use <c:set>to store them commaseparated and use fn:split()to split them for <c:forEach>.

或者,您可以只创建一个成绩集合/数组并将其存储在应用程序范围中,以便它在 EL 中可用,以便您可以使用<c:forEach>. 我不确定那会如何“笨拙”。您可以使用<c:set>逗号分隔存储它们并使用fn:split()它们将它们拆分为<c:forEach>.

<c:set var="grades" value="1,2,3,A,B,C,D,E" scope="application" />
<select class="grade" title="Grade Obtained">
    <c:forEach items="${fn:split(grades, ',')}" var="grade">
        <option value="${grade}" ${qd.grade == grade ? 'selected' : ''}>${grade}</option>
    </c:forEach>
</select>

This way you end up with more DRYcode.

这样你最终会得到更多的DRY代码。