1.首先在jsp頁面select語句下面增長一個隱藏的inputjavascript
<select id="demo" name="demo" onchange="entryChange();"> <option value="">--請選擇--</option> <option value="">1</option> <option value="">2</option> <option value="">3</option> </select> <input id="entryId" name="entryId" type="hidden" value="此處寫你從後臺接收到的值">
2.而後對input框中的值進行賦值,傳給後臺代碼html
function entryChange(){ var entryId= document.getElementById("demo").value; $('#entryId').val(entryId); }
3.賦值以後在後臺定義,而後給他set/get方法,以便在jsp代碼中進行接收java
private String entryId; public String getEntryId() { return entryId; } public void setEntryId(String entryId) { this.entryId = entryId; }
4.從後臺會經過xml仍是別的方式跳轉到jsp頁面,在頁面初始化方法中對傳進來的值進行處理ajax
var entryId = $("#entryId").val(); //這裏根據你本身的需求來進行處理,由於我這裏的數據是用ajax獲取到的值拼接而成的 $.ajax({ contentType:"application/x-www-form-urlencoded;charset=UTF-8", type:"POST", url:"xxxx/xxxxxxxx.action?deptId="+deptId + "&" + Math.random(), dataType:"json", success:function(res){ var ststistic = "<option value=''>"+ "--請選擇--" +"</option>"; for(var i=0;i<res.length;i++){ res[i].statisticId; res[i].statisticName; if(entryId == res[i].statisticId){ ststistic=ststistic+"<option selected='selected' value='"+res[i].statisticId+"'>"+res[i].statisticName+"</option>"; }else{ ststistic=ststistic+"<option value='"+res[i].statisticId+"'>"+res[i].statisticName+"</option>"; } } $("#informationTypeIdEntry").html(ststistic); } });
對循環出來的值進行判斷,若是從後臺傳進來的entryId與循環出來的某個值相同,則在<option>中拼接上 selected='selected'屬性。json
5.還有一種狀況就是下拉框的數據顯示在頁面上是ztree的形式,這種形式使用的不是select標籤,而是input標籤,那麼咱們這裏就還能夠使用另一種回顯方法(上面的都同樣,只不過從後臺返回值的時候作的回顯操做不同):app
//ztree形式的input框 <input id="citySel" name="citySel" readonly type="text" onclick="showMenu(this); return false;"/> var entryId= $("#entryId").val(); if (citySelName.length > 0) { $("#citySel").val(citySelName).trigger("change"); } else { $("#citySel").val(null).trigger("change");//id爲空的話 select框就是空 }
也能夠起到回顯的效果dom