【问题标题】:How to compare two arrays to set html multiselect in jsp如何比较两个数组以在jsp中设置html多选
【发布时间】:2019-01-03 10:22:26
【问题描述】:

我在从数据库中获取的字符串数组中维护了一个来自 html 多选的选定值列表。我想将这些选定选项的值与完整的选择选项列表进行比较,以便在加载屏幕时将它们设置为选中状态。由于循环,我不断获得相同选择选项的多个实例。

    <select id="selectForm" name="selectForm" multiple="multiple"> 
        <c:forEach items="${FullList}" var="fullList">
            <c:forEach items="${PreSelected}" var="preSelected">                
                <option ${preSelected== fullList.name ? 'selected="selected"' : ''}>${fullList.name}</option>
            </c:forEach> 
        </c:forEach>
    </select>

如何获得已选择存储值的单个唯一选择列表?有没有比我的方法更好的解决问题的方法?

【问题讨论】:

    标签: jsp multi-select jsp-tags


    【解决方案1】:

    试试这个,而不是在选项元素之前循环,在选项元素内移动循环,如下所示。

    <select id="selectForm" name="selectForm" multiple="multiple"> 
      <c:forEach items="${FullList}" var="fullList">
        <option 
         <c:forEach items="${PreSelected}" var="preSelected"> 
          <c:if test="${preSelected== fullList.name}">selected</c:if> 
         </c:forEach>
        >${fullList.name}</option>
      </c:forEach>
    </select>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-25
      • 1970-01-01
      • 1970-01-01
      • 2014-04-23
      • 1970-01-01
      • 1970-01-01
      • 2015-11-01
      • 2013-05-02
      相关资源
      最近更新 更多