<s:select>二级联动

来源:互联网 发布:特斯拉知乎 编辑:程序博客网 时间:2024/05/16 08:39


<script type="text/javascript">
$(document).ready(function(){
$("#farmSelect").change(function(){
var farmId=$("#farmSelect").val();
if(farmId == -1){
$("#plotSelect").empty();
$("<option value='-1'>--请选择地块名称--</option>").appendTo($("#plotSelect"));
}
else{
var aurl = "http://localhost:8080/monitor/display/Plot_Check.action";
var adata = "farm_id="+farmId;
$.ajax({
url:aurl,
type:'POST',
data: adata,
dataType : "json",
error:function(e){
alert(e+"----请求失败");
},
success:function(data){
var list = eval(data);
$.each(list, function(i,item){     
$("#plotSelect").empty();
$("<option value='-1'>--请选择地块名称--</option>").appendTo($("#plotSelect"));
$("<option value='"+item.plot_id+"'>"+item.plot_name+"</option>").appendTo($("#plotSelect"));
});
}
});
}
});
});
</script>
<s:form id="yieldList" action="Yield_list" namespace="/display"
method="post" theme="simple">


<table class="editbox">
<tr>
<td class="title"><span class="required">*</span><strong>农场名称:</strong>
</td>
<td><s:select id="farmSelect" name="s_farmName" headerValue="--请选择农场名称--"
headerKey="0" list="%{resultList}" listKey="farm_id"
listValue="farm_name" headerKey="-1"
class="bulk-actions align-left" /></td>
</tr>
<tr>
<td class="title"><span class="required">*</span><strong>地块名称:</strong>
</td>
<td><select id='plotSelect' name="s_plotName"
class="bulk-actions align-left">
<option value="-1">--请选择地块名称--</option>
</select></td>
</tr>
<tr>
<td class="editbox_button" colspan="2"><s:submit value="查询"
cssClass="button" /></td>
</tr>
</table>
</s:form>


java代码


public String check(){
HttpServletRequest request = ServletActionContext.getRequest();  
System.out.println("[farm_id]"+request.getParameter("farm_id"));
String sql= "select * from m_plot " + "where farm_id = '" + request.getParameter("farm_id") + "'";
System.out.println("[select plot]"+sql);
con_DB(sql);
List<Plot> list = new ArrayList<Plot>();
int length = super.getResultList().size();
for(int i=0; i<length;i++){
Plot plot = new Plot();
plot.setFarm_id(super.getResultList().get(i).get("farm_id").toString());
plot.setPlot_id(super.getResultList().get(i).get("plot_id").toString());
plot.setPlot_name(super.getResultList().get(i).get("plot_name").toString());
plot.setPlot_area(super.getResultList().get(i).get("plot_area").toString());
plot.setPlot_addr(super.getResultList().get(i).get("plot_addr").toString());
list.add(plot);
}
System.out.println(list.get(0).getFarm_id());
try {
Gson gson = new Gson();  
String s2 = gson.toJson(list); 
System.out.println("json="+s2);
ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");//解决中文乱码问题
ServletActionContext.getResponse().getWriter().print(s2);
} catch (IOException e) {
e.printStackTrace();
}  
return null;
}


action代码:

<action name="Plot_Check" class="test.ListPlotCheckAction" method="check">

</action>

0 0
原创粉丝点击