如何使用 JSP 传递复选框数据?


当需要选择多个选项时,可以使用复选框。

下面是一个HTML 代码示例,CheckBox.htm,用于一个包含两个复选框的表单。

<html>
   <body>
      <form action = "main.jsp" method = "POST" target = "_blank">
         <input type = "checkbox" name = "maths" checked = "checked" /> Maths
         <input type = "checkbox" name = "physics" /> Physics
         <input type = "checkbox" name = "chemistry" checked = "checked" /> Chemistry
         <input type = "submit" value = "Select Subject" />
      </form>
   </body>
</html>

以上代码将生成以下结果 −

 数学 物理 化学

以下是用于处理复选框按钮由 Web 浏览器输入的 main.jsp JSP 程序。

<html>
   <head>
      <title>Reading Checkbox Data</title>
   </head>
   <body>
      <h1>Reading Checkbox Data</h1>
      <ul>
         <li><p><b>Maths Flag:</b>
            <%= request.getParameter("maths")%>
            </p></li>
         <li><p><b>Physics Flag:</b>
            <%= request.getParameter("physics")%>
            </p></li>
         <li><p><b>Chemistry Flag:</b>
            <%= request.getParameter("chemistry")%>
            </p></li>
      </ul>
   </body>
</html>

以上程序将生成以下结果 −

读取复选框数据

  • 数学标记:: 已打开

  • 物理标记:: null

  • 化学标记:: 已打开

更新于: 2019 年 7 月 30 日

已查看 4K 次

开启您的 职业生涯

通过完成课程获得认证

开始
广告
© . All rights reserved.