使用jstl在现有选择标记上设置选定选项 [英] Set selected option on existing select tag with jstl
问题描述
c为C:的forEach项= $ {qualificationdetails} VAR = QD >
对于每个项目,我都会生成一个像这样的选择。
< ; select class =gradetitle =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>
是否可以使用我的变量 qd 设置选定的选项?类似于
< option value =$ {qd.grade}selected =selecetd> $ {qd。级}< /选项>
这将为select添加一个重复选项。我认为这将有点笨重,与成绩组成一个阵列,并把它发送到生成选项。任何想法?
您可以让JSP呈现选定的
属性
< select class =gradetitle =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>
或者,您可以创建一个集合/数组并将其存储在应用程序范围中,它在EL中可用,以便您可以使用< c:forEach>
来遍历它。我不确定那会是多么笨重。您可以使用< c:set>
来存储它们的逗号分隔,并使用 fn:split()
< c:forEach>
。
< c:set var =gradesvalue =1,2,3,A,B,C,D,Escope =application/>
< option value =$ {grade}$ {qd.grade == grade? 'selected':''}> $ {grade}< / option>
< / c:forEach>
< / select>
通过这种方式,您可以获得更多 DRY 代码。
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">
.
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
<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 ?
You could just let JSP render the selected
attribute conditionally.
<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>
.
<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 DRY code.
这篇关于使用jstl在现有选择标记上设置选定选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!