预先填充JSP中的单选按钮 [英] Pre-populating radio buttons in JSP

查看:84
本文介绍了预先填充JSP中的单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用JSP预填充HTML单选按钮,具体取决于数据库中的值?

How can I pre-populate a HTML radio button using JSP, depending on the value in the database?

推荐答案

您只需让JSP打印HTML < input type =radio> 元素的选中的属性。最简单的方法是在 EL ?: >。这是一个开球的例子:

You just need to let JSP print the checked attribute of the HTML <input type="radio"> element. Easiest way to do that is using the conditional operator ?: in EL. Here's a kickoff example:

<input type="radio" name="foo" value="one" ${bean.foo == 'one' ? 'checked' : ''}/>
<input type="radio" name="foo" value="two" ${bean.foo == 'two' ? 'checked' : ''}/>
...

或者如果您在某些集合中拥有所有可用的输入值,如 List< String> ,然后执行:

Or if you have all available input values in some collection like List<String>, then do:

<c:forEach items="${foos}" var="foo">
  <input type="radio" name="foo" value="${foo}" ${bean.foo == foo ? 'checked' : ''}/>
</c:forEach>

无论哪种方式,如果最终会在生成的HTML中最终结束, $ {bean.foo} 等于two

Either way, it should ultimately end up as follows in the generated HTML if ${bean.foo} equals to "two":

<input type="radio" name="foo" value="one" />
<input type="radio" name="foo" value="two" checked />
...



另见:



查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆