使用数据库中的值设置 jsp 复选框
好的。我正在制作一个带有数据库后端的 java web 应用程序,以对某些数据执行一些 CRUD 操作。单击项目旁边的编辑按钮时,它将导航到包含当前数据的表单以进行编辑。其中一个字段是布尔值,我想将其显示为复选框,以便 True
使其选中,False
使其不选中。
我尝试了许多不同的变体,似乎都不起作用。以下是一些示例,其中 <%= action.get("stable")%>
返回带有 True
或 False
的字符串,
<input TYPE=checkbox name="stable" value=<%= action.get("stable") %>
<input TYPE=checkbox name="stable" value=<%= action.get("stable")?"True":"False" %><%= action.get("stable")?"checked":"" %>
<input TYPE=checkbox name="stable" checked=<%= action.get("stable")%>/>
因此如何根据 action.get("stable") 返回的字符串将复选框设置为选中/未选中状态
感谢您的帮助,如果问题有点微不足道,抱歉。
Ok. I'm making a java web app with a database backend to do some CRUD on some data. When the edit button is clicked next to an item, it navigates to a form with the current data for editing. One of the fields is boolean and I would like to display it as a checkbox so that True
makes it checked and False
leaves it unchecked.
I have tried many different variations none seem to work. Here are some examples where <%= action.get("stable")%>
returns a string with either True
or False
<input TYPE=checkbox name="stable" value=<%= action.get("stable") %>
<input TYPE=checkbox name="stable" value=<%= action.get("stable")?"True":"False" %><%= action.get("stable")?"checked":"" %>
<input TYPE=checkbox name="stable" checked=<%= action.get("stable")%>/>
So how do you set a check box to checked/unchecked depending on the string returned with action.get("stable")
Thank you for any help sorry if the question is a bit trivial.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我用过这个,效果很好。
I used this, and it worked perfectly.
已选中复选框的正确标记是
checked="checked"
。如果未选中,则checked
属性根本不能存在。您应该使用 JSTL 和 JSP EL 生成它,因为 scriptlet 是过去的东西,多年来不应该在 JSP 中使用。请参阅如何避免在 JSP 文件中出现 Java 代码?。
这当然需要一些重构,以便操作 bean 具有返回布尔值的常规
isStable()
方法,这会更清晰。但无论如何,以下是使用现有代码的工作方式:请注意,所有属性也应该用引号引起来。
The correct markup for a checked checkbox is
checked="checked"
. If it's not checked, thechecked
attribute must not be present at all.You should generate it using the JSTL and the JSP EL, because scriptlets are something from the past which should not be used in JSPs for years. See How to avoid Java code in JSP files?.
This would of course need some refactoring so that the action bean has a regular
isStable()
method returning a boolean, which would be much cleaner. But anyway, here's how it would work using your existing code :Note that all attributes should also be surrounded by quotes.
您需要设置
的
checked
属性编辑:
You need to set
checked
attribute of<input type="checkbox"/>
Edit: