获得select的value和text示例

<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>select表单测试</title>
</head>
<%
    String baobiao = request.getParameter("baobiao");//获得下拉选中被选中的option的value
    if (baobiao == null || baobiao.equalsIgnoreCase(""))
        baobiao = "sryszxqkb";//如果为空,给它赋个默认值
    String baobiaotext = request.getParameter("baobiaotext");//获得下拉选中被选中的option的text
    if (baobiaotext == null || baobiaotext.equalsIgnoreCase(""))
        baobiaotext = "收入预算及执行情况表";//如果为空,给它赋个默认值
   
    //打印下,看看是否获得到了
    System.out.println("baobiao============" + baobiao);
    System.out.println("baobiaotext============" + baobiaotext);
%>

<body>
<form action=a.jsp method=get>
<select name="baobiao"
onChange="baobiaotext.value=this.options[this.selectedIndex].text;">
</select>
<input type=hidden name="baobiaotext" value="">

<td>
<input class="ui_ent" type=submit value="确定">
</td>
</form>
</body>
</html>
<script language="javascript">
   var baobiao = "sryszxqkb;收入预算及执行情况表;zcqkb_jd;资产情况表;zcfzb;资产负债简表;xmszyszxqkb;项目收支预算执行情况表;zcyszxqkzb;支出预算及执行情况表;jbzcyszxqkb;基本支出预算执行情况表";
   var genderArrSdt = baobiao.split(";"); //截串放入数组
   //构造select
   for(var i=0;i<genderArrSdt.length;i+=2)
   {       
      document.getElementById("baobiao").add(new Option(genderArrSdt[i+1],genderArrSdt[i])); //前边是value,后边是text              
   }
  
   //设置被选中
   var myval= "<%=baobiao%>";
   //alert(myval);
   var objSelect = document.getElementById("baobiao");  
   for(var i=0;i<objSelect.options.length;i++)
   {        
      if(objSelect.options[i].value == myval)
      {
         objSelect.options[i].selected = true;
         break;
      }
    } 
</script>

你可能感兴趣的:(html)