select下拉框,选择其中一个,而后进行查询,完成以后,页面上的select框不回显当前查询时选中的值

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

相关文章
相关标签/搜索