如何显示基于选定单选按钮的下拉结果JSP

时间:2018-08-28 21:46:01

标签: java jsp struts2 jstl

我正在尝试编写一个JSP页面,该页面需要根据同一页面上的“选择国家/地区”单选按钮显示状态的下拉列表。当我运行该页面时,即使选择了USA单选按钮,它也会显示c:otherdown下拉列表。请帮助

USA:<html:radio property="country" value="country" >
<c:set var ="p" value="usa"/>
</html:radio>

Germany:<html:radio property="country" value="country" >
<c:set var ="p" value="germany"/>
</html:radio>

<br>
list:
<c:choose>
<c:when  test = "${p == processors }" >

//drop down for states in USA
</c:when>
<c:otherwise >
//drop down for states in germany
</c:otherwise>
</c:choose>
enter code here

1 个答案:

答案 0 :(得分:0)

您必须以某种方式将用户的选择传播到服务器端,因为使用JSP,您主要可以采用这种方式。一个有效的示例:

<%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>
<HTML>
    <HEAD>
        <TITLE>Reading Radio Buttons</TITLE>
    </HEAD>

    <BODY>
        <H1>Reading Radio Buttons</H1>
        <%
            boolean radio2checked = false;
            if(request.getParameter("radios") != null) {
                if(request.getParameter("radios").equals("radio1")) {
                    out.println("Radio button 1 was selected.<BR>");
                }
                else {
                    out.println("Radio button 1 was not selected.<BR>");
                }
                if(request.getParameter("radios").equals("radio2")) {
                    out.println("Radio button 2 was selected.<BR>");
                    radio2checked = true;
                }
                else {
                    out.println("Radio button 2 was not selected.<BR>");
                }
            }
            pageContext.setAttribute("showUSAdropdown", radio2checked);
            String checked = radio2checked ? "checked=\"checked\"" : "";
        %>
        <FORM name="radioform" METHOD="post">
             <INPUT TYPE="radio" onclick="document.radioform.submit()" NAME="radios"     VALUE="radio1" CHECKED>
             Radio Button 1
            <BR>
            <INPUT TYPE="radio" <%=checked%> onclick="document.radioform.submit()" NAME="radios" VALUE="radio2">
             Radio Button 2
            <BR>
            States:
            <SELECT name="states">
             <c:choose>
                <c:when test="${showUSAdropdown}">
                    <option>Alabama</option>
                    <option>Washington</option>
                </c:when>
                <c:otherwise>
                    <option>Germany</option>
                </c:otherwise>
            </c:choose>
            </SELECT>
        </FORM>
    </BODY>
</HTML>