勾选复选框如果复选框中值的ArrayList等于值 [英] Checking a checkbox if checkbox value equals value in an arrayList

查看:218
本文介绍了勾选复选框如果复选框中值的ArrayList等于值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我用我所谓的selectedModsJSP文件值的ArrayList。 selectedMods包含了$ P $的价值形式提交前pviously选中的复选框。我想为形式被刷新时的复选框保持选中状态。我在这里的思路是,每个复选框项,遍历pviously选中复选框$ P $的数组列表selectedMods,如果该复选框值等于任何selectedModspviously选择了$ P $,那么检查复选框。这里的code显示的复选框:

I have an arrayList of values I'm using in my jsp file called "selectedMods". "selectedMods" contains the values of the previously checked checkboxes before form submission. I'd like for the checkboxes to remain checked when the form is refreshed. My line of thought here is that for each checkbox item, traverse the arraylist "selectedMods" of previously selected checkboxes and if that checkbox value equals any of the previously selected in "selectedMods" then check that checkbox. Here's the code for displaying the checkboxes:

 <c:forEach items="${modules}" var="module">
        <label class="checkbox"><input type="checkbox" name="moduleIds value="${module.id}"  /> ${module.title}</label>
 </c:forEach>

在值我想过将是这样的:

After "value" I thought about adding something like this:

 <c:if test="${selectedMods == ${module.id}">checked="checked"</c:if>

但我知道不用遍历链表,我不能比较的单个值值的列表,我不知道从哪里开始。任何帮助深表AP preciated。

But I realize I can't compare a list of values against a single value without traversing the list and I'm not sure where to start. Any help is much appreciated.

推荐答案

您可以构造图的检查选择输入的,其中一个入口的关键将是一个模块ID,比你的病情会是这样

You can construct a map of checked select inputs, where the key of an entry would be a module id, than your condition would be something like

<c:if test="${selectedMods.containsKey(module.id)}">

这篇关于勾选复选框如果复选框中值的ArrayList等于值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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