1. 程式人生 > >在JSP頁面獲取js中的變數值

在JSP頁面獲取js中的變數值

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'test.jsp' starting page</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
<script type="text/javascript">
function get(){
    document.getElementById("sel").value=document.getElementById("pull_down").value;
    document.getElementById("myForm").submit();
}
</script>
  </head>

  <body>
    同一頁面中JSP獲取JS取到的值 <br>
    下拉框:<br/>
    <select id="pull_down" onchange="get()">
    <option>請選擇</option>
    <option value="1">一</option>
    <option value="2">二</option>
    <option value="3">三</option>
    </select>
    <form action="main/test.jsp" method="post" id="myForm">
    <input type="hidden" id="sel" name="sel"/>
    </form>
    <%
    out.print("JSP獲取下拉框的value值:"+request.getParameter("sel"));
    %>
  </body>
</html>