天天看点

根据传入值,自动显示标签 select 下拉选项

第一种方法:

1. jsp页面中得标签 <select 

 <td>

    <span class="style1">&nbsp;&nbsp;&nbsp;&nbsp;</span>

    <select name="fatherid" >

        <option value="1">类型1        </option>

        <option value="2">类型2

        </option>

    </select>

</td>

2. javaScript:

function selMoreValue(obj,para)

{

  var lenSel = eval(obj+".length");

  for(var i = 0;i<lenSel;i++)

  {

    if(eval(obj+".options["+i+"].value") == para)

    {

      eval(obj+".options["+i+"]").selected = true;

    }

  }

}

然后在 jsp页面中,

<body onLoad="javascript:selMoreValue('document.all.fatherid','<%=b.getFatherid()%>');">

若有多个select可以再写的pageonload(),只为调用selMoreValue();

function pageonload(){

selMoreValue('document.all.fatherid','<%=b.getFatherid()%>');

selMoreValue('document.all.fatherid2','<%=b.getFatherid2()%>');

……

}

<body onLoad="pageonload()">

第二种方法:

若<option>是jsp页面中循环输出的,还可以这样,若该项value==b.getFatherid()直接out.print("selected");

<%

  List list;

  list = (List)request.getAttribute("gradedatelst");

  Iterator it = list.iterator();

 %>

 <td>

    <span class="style1">&nbsp;&nbsp;&nbsp;&nbsp;</span>

    <select name="fatherid" >

<%

     while(it.hasNext()){         

      Bean gb = (Bean)it.next();

%>

        <option value="1">类型1        </option>

 <option value="<%=gb.sltvalue()%>" <%if(gb.sltvalue()==b.getFatherid()){out.print("selected")})%>><%=gb.sltvalue()%></option>

       <%

    }

    %>

       </select>

</td>