下面是我的代码(1.jsp)
<html>
<head>
<script type="text/javascript">
function changeFunc() {
var selectBox = document.getElementById("selectBox");
var selectedValue = selectBox.options[selectBox.selectedIndex].value;
document.write("\n value is"+selectedValue);
}
</script>
</head>
<body>
<form method="post" action="SampServlet">
<select id="selectBox" name="selurl" onchange="changeFunc();">
<option value="1">Option #1</option>
<option value="2">Option #2</option>
</select>
</form>
</body>
</html>
在这里,我已将此代码插入到一个 jsp 页面中。并像这样在同一个 jsp 中将“selectedValue”的值从 javascript 获取到 scriptlet。
<% String val=(String)request.getParameter("selurl");
System.out.println("\n selected value is:"+val); %>
我将选定的值作为空值作为输出。如果我打印 javascript selectedValue 参数,它会给我正确的输出,即输出作为选择的选项。但在 scriptlet 中,我得到了 null。错误在哪里。我包括了所有标题和指令。请帮助我。