使用jstl foreach标签检查单选按钮

时间:2014-06-12 04:47:13

标签: java jsp jstl

我有一张地图(${userOptions})。例如{1=B, 2=B, 3=D, 4=A, 5=D}nxtId包含1或2或3等。

如果<c:if test="${useroption.key == nxtId}">返回成功,请检查变量value是否包含A或B或C或D.如果值为&#39; A&#39;必须检查相应的单选按钮,并且必须取消选中剩余按钮等等。

我目前正在使用以下内容:

<c:forEach var="useroption" items="${userOptions}">
    <c:out value="${useroption.key == nxtId}"/>
    <script>
        alert("${useroption.key == nxtId}");
    </script>
    <c:if test="${useroption.key == nxtId}">
    <c:set var="check" value="true"/>
    <c:set var="value" value="${useroption.value}"/>
    </c:if>
    <script>
        alert("check value is"+${check});
    </script>
</c:forEach>

<c:choose>
    <c:when test="${check eq true}">
        <script>
            alert("user option is "+${useroption.value});
        </script>
        <c:forEach var="opt" items="${questionForm}">                                                                                                         
            <c:choose>
                <c:when test="${value == 'A' }">
                    <tr>
                        <td style="padding-left:200px;">                                                                                      
                        <input type="hidden" name="id" id="id" value="${opt.question_id}"/>
                        <h4>Q <c:out value="${opt.question_id}"/>.<c:out value="${opt.question}"/></h4>

                        <input type="radio" name="option" value="A" id="A" checked="checked">

                        A. <c:out value="${opt.option1}"/><br>
                        <input type="radio" name="option" value="B" id="B">
                        B.  <c:out value="${opt.option2}"/><br>
                        <input type="radio" name="option" value="C" id="C">
                        C.  <c:out value="${opt.option3}"/><br>
                        <input type="radio" name="option" value="D" id="D">
                        D.  <c:out value="${opt.option4}"/><br><br>

                        </td>
                    </tr>   
                    <tr align="center">
                        <td>
                        <c:if test="${opt.question_id > 6}">
                        <input type="submit" value="submit"/>
                        </c:if>
                        </td>
                    </tr>                     
                </c:when>
                <c:when test="${value == 'B' }">
                    <tr>
                        <td style="padding-left:200px;">                                                                                      
                        <input type="hidden" name="id" id="id" value="${opt.question_id}"/>
                        <h4>Q <c:out value="${opt.question_id}"/>.<c:out value="${opt.question}"/></h4>

                        <input type="radio" name="option" value="A" id="A" >

                        A. <c:out value="${opt.option1}"/><br>
                        <input type="radio" name="option" value="B" id="B" checked="checked">
                        B.  <c:out value="${opt.option2}"/><br>
                        <input type="radio" name="option" value="C" id="C">
                        C.  <c:out value="${opt.option3}"/><br>
                        <input type="radio" name="option" value="D" id="D">
                        D.  <c:out value="${opt.option4}"/><br><br>

                        </td>
                    <tr align="center">
                        <td>
                        <c:if test="${opt.question_id > 6}">
                        <input type="submit" value="submit"/>
                        </c:if>
                        </td>
                    </tr>                     
                </c:when>
                <c:when test="${value == 'C' }">
                    <tr>
                        <td style="padding-left:200px;"> 
                        <input type="hidden" name="id" id="id" value="${opt.question_id}"/>
                        <h4>Q <c:out value="${opt.question_id}"/>.<c:out value="${opt.question}"/></h4>

                        <input type="radio" name="option" value="A" id="A" >

                        A. <c:out value="${opt.option1}"/><br>
                        <input type="radio" name="option" value="B" id="B">
                        B.  <c:out value="${opt.option2}"/><br>
                        <input type="radio" name="option" value="C" id="C" checked="checked">
                        C.  <c:out value="${opt.option3}"/><br>
                        <input type="radio" name="option" value="D" id="D">
                        D.  <c:out value="${opt.option4}"/><br><br>

                        </td>
                    <tr align="center">
                        <td>
                        <c:if test="${opt.question_id > 6}">
                        <input type="submit" value="submit"/>
                        </c:if>
                        </td>
                    </tr>                     
                </c:when>
                <c:when test="${value == 'D' }">
                    <tr>
                        <td style="padding-left:200px;">                                                                                      
                        <input type="hidden" name="id" id="id" value="${opt.question_id}"/>
                        <h4>Q <c:out value="${opt.question_id}"/>.<c:out value="${opt.question}"/></h4>

                        <input type="radio" name="option" value="A" id="A" >

                        A. <c:out value="${opt.option1}"/><br>
                        <input type="radio" name="option" value="B" id="B" >
                        B.  <c:out value="${opt.option2}"/><br>
                        <input type="radio" name="option" value="C" id="C">
                        C.  <c:out value="${opt.option3}"/><br>
                        <input type="radio" name="option" value="D" id="D" checked="checked">
                        D.  <c:out value="${opt.option4}"/><br><br>

                        </td>
                    <tr align="center">
                        <td>
                        <c:if test="${opt.question_id > 6}">
                        <input type="submit" value="submit"/>
                        </c:if>
                        </td>
                    </tr>                     
                </c:when>           
                <c:otherwise>
                    <script>
                        alert("hello");
                    </script>
                        <td style="padding-left:200px;">                                                                                      
                        <input type="hidden" name="id" id="id" value="${opt.question_id}"/>
                        <h4>Q <c:out value="${opt.question_id}"/>.<c:out value="${opt.question}"/></h4>
                        <input type="radio" name="option" value="A" id="A" >

                        A.  <c:out value="${opt.option1}"/><br>
                        <input type="radio" name="option" value="B" id="B">
                        B.  <c:out value="${opt.option2}"/><br>
                        <input type="radio" name="option" value="C" id="C" >
                        C.  <c:out value="${opt.option3}"/><br>
                        <input type="radio" name="option" value="D" id="D">
                        D.  <c:out value="${opt.option4}"/><br><br>
                        </td>
                    <tr align="center">
                        <td>
                        <c:if test="${opt.question_id > 6}">
                        <input type="submit" value="submit"/>
                        </c:if>
                        </td>
                    </tr>                     
                </c:otherwise>
            </c:choose>
        </c:forEach>
    </c:when>
<c:otherwise>
</c:otherwise>
</c:choose>

有更好的方法吗?

2 个答案:

答案 0 :(得分:0)

您可以在不重复

之类的情况下完成此操作
A.  <c:out value="${opt.option1}"/><br>
<input type="radio" name="option" value="B" id="B" "${value == 'B' ? 'checked' : ''}">
B.  <c:out value="${opt.option2}"/><br>
<input type="radio" name="option" value="C" id="C" "${value == 'C' ? 'checked' : ''}">
C.  <c:out value="${opt.option3}"/><br>
<input type="radio" name="option" value="D" id="D" "${value == 'D' ? 'checked' : ''}" >
D.  <c:out value="${opt.option4}"/><br><br>

答案 1 :(得分:0)

A.  <c:out value="${opt.option1}"/><br>
<input type="radio" name="option" value="B" id="B" ${value eq 'B' ? 'checked' : ''}>
B.  <c:out value="${opt.option2}"/><br>
<input type="radio" name="option" value="C" id="C" ${value eq 'C' ? 'checked' : ''}>
C.  <c:out value="${opt.option3}"/><br>
<input type="radio" name="option" value="D" id="D" ${value eq 'D' ? 'checked' : ''} >
D.  <c:out value="${opt.option4}"/><br><br>

从上面的代码中你可以实现你想要的...... 通过使用JSTL和三元运算符,您可以用更少的代码实现功能 希望它对你有所帮助