如何实现此代码:Hashmaps,<s: 迭代器>

How to implement this code: Hashmaps, <s:iterator>

本文关键字:lt 何实现 gt 迭代器 Hashmaps 实现 代码      更新时间:2023-09-26

寻找实现以下功能的最佳方法的建议:

我在准备我的动作课时有一个主题图。这个hashmap有两个值,FruitType和一个Boolean。可用的水果类型取决于使用屏幕的人。布尔值还取决于使用屏幕的人。

在我的JSP中,我有两种形式:

第一种形式具有包含可用水果类型的<s:select>列表。只有当对应于水果类型的布尔值为true时,才需要第二种形式。如果布尔值为true,则启用第二个表单,并禁用第一个表单上的提交按钮。如果布尔值为false,则禁用第二个表单,并启用第一个表单上的提交按钮。

我对通过javascript启用/禁用没有任何问题。我的问题是将布尔值存储在哪里。。。我在第一个表单中有一个隐藏的输入(isSecondFormReq)和一个在我的选择列表上的帖子(当选择值时触发),它可以计算水果布尔值是真是假,但我无法更新我的隐藏输入字段的值。。。

有人有更好的方法吗?

非常感谢。

编辑:我认为最好的方法可能是使用<s:iterator>,并隐藏id=键和value=值的字段。。。如有任何帮助,我们将不胜感激:)

最后读了三遍,我猜你自己已经提出了一个解决方案,如果FruitType你可以使用你提到的解决方案,即

create a map for fruittype,boolean and then iterate over it in the JSP, so that the 2nd form gets displayed based on the value of the selected fruittype and it's corresponding boolean

我不知道你是如何实现javascript的,但为了这个答案的完整性,我想我会提供一个小样本。

<select name="fruit" id="fruit">
<s:iterator value="mapFruitBoolean">
   <option value="fruit.key" data-boolean=<s:property value="value"/>>>Fruit.value</option>
<!-- Above fruit refers to the key of the map, and value is just a boolean
Map<Fruit,Boolean> -->
</s:iterator>
</select>

Javascript代码

$("#fruit").change(function(){
    var option = $(this).find('option:selected');
    if($(option).attr('data-boolean')){
       //Enable the submit button
    }else{
     //Disable the submit button
    }
});

上面的代码未经测试,但我想它应该可以正常工作,尽管我想它清楚地解释了这个概念。