JSP中无法获取下拉框选中值问题求助
问题原因分析
你遇到的核心问题是:前端JavaScript里的selectedstate变量只存在于浏览器内存中,没有把值提交到服务器;而且你的下拉框没有设置name属性,就算提交表单,服务器也无法通过request.getParameter获取到值。
解决方案
下面给两种常用的实现方式,按需选择:
方式1:表单提交(页面刷新)
这种方式最简单,把下拉框和输入框放到表单里,选完自动提交或者手动提交:
- 修改HTML结构,添加表单并给下拉框设置
name属性:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <% String thisstate=request.getParameter("selectedstate"); if(thisstate != null){ System.out.println("the selected state is " + thisstate); } %> <form method="post" action=""> <select id="State" name="selectedstate" onchange="getstate()"> <option value="'%'">All</option> <option value="228">Move</option> <option value="212">Clean</option> <option value="196">Cleaning</option> <option value="180">Ready</option> <option value="164">Occupied</option> </select><br><br> TextBox1<br> <input type="text" id="srt" placeholder="get value on option select"><br> <!-- 可选:手动提交按钮,不想自动提交就去掉select的onchange里的提交逻辑 --> <input type="submit" value="提交"> </form> <script> function getstate() { var selectedVal = document.getElementById("State").value; document.getElementById("srt").value = selectedVal; // 选完自动提交表单 document.querySelector("form").submit(); } </script>
- 关键说明:
- 下拉框必须设置
name="selectedstate",服务器端request.getParameter("selectedstate")是根据name取值,不是id - 表单提交后,页面会刷新,服务器就能拿到选中值,后续可以直接用
thisstate变量,或者用JSTL的${param.selectedstate}获取参数
- 下拉框必须设置
方式2:AJAX异步提交(无页面刷新)
如果不想刷新页面,用AJAX把值异步发给服务器:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <% String thisstate=request.getParameter("selectedstate"); if(thisstate != null){ System.out.println("the selected state is " + thisstate); // 如果需要给前端返回响应,可以在这里输出内容 // out.print("收到值:" + thisstate); } %> <select id="State" onchange="getstate()"> <option value="'%'">All</option> <option value="228">Move</option> <option value="212">Clean</option> <option value="196">Cleaning</option> <option value="180">Ready</option> <option value="164">Occupied</option> </select><br><br> TextBox1<br> <input type="text" id="srt" placeholder="get value on option select"><br> <script> function getstate() { var selectedVal = document.getElementById("State").value; document.getElementById("srt").value = selectedVal; // 发送AJAX请求 var xhr = new XMLHttpRequest(); xhr.open("POST", "", true); xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); xhr.onreadystatechange = function() { if (xhr.readyState === 4 && xhr.status === 200) { // 可以在这里处理服务器返回的内容,比如显示提示 console.log("服务器已收到值:" + xhr.responseText); } }; // 把参数编码后发送,避免特殊字符问题 xhr.send("selectedstate=" + encodeURIComponent(selectedVal)); } </script>
JSTL使用示例
拿到值后,直接用${param.selectedstate}就能在JSTL标签里使用,比如:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <c:choose> <c:when test="${param.selectedstate == '228'}"> 当前选中状态:Move </c:when> <c:when test="${param.selectedstate == '212'}"> 当前选中状态:Clean </c:when> <c:otherwise> 当前选中状态:${param.selectedstate == "'%'" ? 'All' : param.selectedstate} </c:otherwise> </c:choose>
内容的提问来源于stack exchange,提问作者Ralph
相关产品推荐
相关产品推荐

