multiple radio button selection

被刻印的时光 ゝ 提交于 2019-12-24 09:31:10

问题


I've to display many groups and many products under each group. For displying them I'm using JSTL to iterate the list of products from the list of groups. User can select one product from each group by clicking on the radio. To enable this i've added the id of the group with the radio name, so that user can select multiple radios.

How can get the selected radios from the servlet? Because the name is created dynamically.

<c:forEach items="${pgb.tableValues}" var="tv">
   <tr>
     <c:forEach items="${tv}" var="tvalue">
        <c:if test="${tvalue.type != null && not empty(tvalue.type)}">
           <td>
            <c:if test="${tvalue.type=='radio'}">
              <input type="radio" value="${tvalue.id}" name="selectedProd${pgb.id}"/>
            </c:if>
            <c:if test="${tvalue.image != null}">
                <img src="${tvalue.image}" alt="image"/>
            </c:if>
            ${tvalue.text}
          </td>
        </c:if>
     </c:forEach>
    </tr>
</c:forEach>

or is there some better way to do this?


回答1:


Just get them by same name as you specified in the HTML.

String selected = request.getParameter("selectedProd" + pgb.getId());

By the way, the ${tvalue.type != null && not empty(tvalue.type)} can be shortened to ${not empty tvalue.type}. The empty also checks for null.



来源:https://stackoverflow.com/questions/3184959/multiple-radio-button-selection

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!