Java Tutorial/JSP/Form CheckBox — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
 
м (1 версия)
 
(нет различий)

Текущая версия на 05:07, 1 июня 2010

Check the CheckBox in your code based on the selection on RadioButton

<HTML>
    <HEAD>
        <TITLE>Design Your Car!</TITLE>
    </HEAD>
 
    <BODY>
    <% 
        String aCar = "", bCar = "", cCar = "";
        String check1 = "", check2 = "", check3 = "";
        if(request.getParameter("radios") != null) {
           if(request.getParameter("radios").equals("aCar")){
               aCar = "CHECKED";
               check2 = "CHECKED";
           }
           if(request.getParameter("radios").equals("bCar")){
               bCar = "CHECKED";
               check1 = "CHECKED";
               check3 = "CHECKED";
           }
           if(request.getParameter("radios").equals("cCar")){
               cCar = "CHECKED";
               check1 = "CHECKED";
               check2 = "CHECKED";
               check3 = "CHECKED";
           }
       }
    %>
        <H1>Design Your Car!</H1>
        <FORM METHOD="POST">
            <TABLE BORDER="1" ALIGN="LEFT">
                <TR>
                    <TH>
                        Car
                    </TH>
                </TR>
                <TR>
                    <TD>
                        <INPUT TYPE="radio" NAME="radios" VALUE="aCar"
                            <%= aCar %>
                        >
                       Cheese Car
                       <BR>
                       <INPUT TYPE="RADIO" NAME="radios" VALUE="bCar"
                           <%= bCar %>
                       >
                       Turkey Car
                       <BR>
                       <BR>
                       <INPUT TYPE="RADIO" NAME="radios" VALUE="cCar"
                            <%= cCar %>
                       >
                       Ham Car
                       <BR>
                   </TD>
               </TR>
            </TABLE>
    
            <TABLE BORDER="1" ALIGN="LEFT">
                <TR>
                    <TH>
                        Colors
                    </TH>
                </TR>
                <TR>
                    <TD>
                        <INPUT TYPE="CHECKBOX" NAME="check1" VALUE="check1"
                            <%= check1 %>
                        >
                        Red
                        <BR>
                        <BR>
                        <INPUT TYPE="CHECKBOX" NAME="check2" VALUE="check2"
                            <%= check2 %>
                        >
                        Yellow
                        <BR>
                        <BR>
                        <INPUT TYPE="CHECKBOX" NAME="check3" VALUE="check3"
                            <%= check3%>
                        >
                        Black
                    </TD>
                </TR>
            </TABLE>
            <BR CLEAR="ALL">
            <BR>
            <INPUT TYPE="SUBMIT" VALUE="Submit">
        </FORM>
    </BODY>
</HTML>





Read Form Checkboxes

index.jsp



<HTML>
    <HEAD>
        <TITLE>Submitting Check Boxes</TITLE>
    </HEAD>
 
    <BODY>
        <H1>Submitting Check Boxes</H1>
        <FORM ACTION="basic.jsp" METHOD="post">
             <INPUT TYPE="CHECKBOX" NAME="check1" VALUE="check1" CHECKED>
             Checkbox 1
            <BR>
            <INPUT TYPE="CHECKBOX" NAME="check2" VALUE="check2">
            Checkbox 2
            <BR>
            <INPUT TYPE="CHECKBOX" NAME="check3" VALUE="check3">
            Checkbox 3
            <BR>
            <INPUT TYPE="SUBMIT" VALUE="Submit">
        </FORM>
    </BODY>
</HTML>