1. 程式人生 > >HTML實現單項選擇,並輸出選項名。

HTML實現單項選擇,並輸出選項名。

<%@ 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 'index.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 save() {
var index=document.getElementById("single").selectedIndex;//得到選型索引
var cityName=document.getElementById("single").options[index].text;//得到選項內容
alert(cityName);
}
</script>
  </head>
  
  <body>
      <select size="3" name="single" id="single">
        <option value="1" selected="selected" id="bj">北京</option>
        <option value="2" id="hz">杭州</option>
        <option value="3" id="cq">重慶</option>
      </select>
      <input type="submit" value="儲存" onclick="save()">
  </body>
</html>