首頁  >  文章  >  web前端  >  基於JQuery實作的Select級聯_jquery

基於JQuery實作的Select級聯_jquery

WBOY
WBOY原創
2016-05-16 17:02:041057瀏覽
複製程式碼 程式碼如下:

$(document).ready(function(){

$(document).ready(function(){


$("#precinct").change(function(){
$("#ptype").val("");//重設所有
$("#stype" ).html("");
$("#stype").append("");
});

//監聽專利類型change事件
$("#ptype").change(function(){
var ptype = $(this);
var atype = $(this).val();//物件值
var pid = $("#precinct").val();
if (!ptype. data(atype)) {//從快取拿到值得話不需要再和伺服器互動
$.post("Main/PatentSubsidy/getSubsidy",{askfor:atype,precinct:pid},function(json) {//返回json物件
$("#stype").html("");//清空#stype下拉框
for(var i=0;i//新增一個
$("#stype").append("");
};
ptype.data(atype, json); //以#ptype的值為key加入到快取
},'json');
}else{
var json = ptype.data(atype);//取快取
$("#stype").html("" );
for(var i=0;i//增加一個
$("#stype").append("");
};
}
});
});


根據#precinct和#ptype取得#stype
action method 複製代碼
代碼如下:


public void getSubsidy(){
String askfor=null,precinct=null;
if(null!=getPara("askfor")&&!"".equals(getPara ("askfor"))){
askfor=getPara("askfor");
if(null!=getPara("precinct")&&!"".equals(getPara("precinct"))){
precinct=getPara("precinct");
}
}else{
renderJson("[{"id":"","value":"--請選擇--"} ]");//傳空值,回傳
}
String sql = "select s.id, s.subsidy_type, p.name from org_subsidy_flow s, tab_precinct p where s.enabled = 'true' and p .status = '1' and s.patent_type = '" askfor "' and s.precinct = p.id";
if(null!=precinct&&!"".equals(precinct)){
sql = " 和 p.id = " precinct;
}
sql = " order by p.id, s.id";
List sf = Org_subsidy_flow.dao.find(sql);
if(sf.size()!=0){
StringBuffer buffer = new StringBuffer();
for(int i=0;ibuffer. append("{"id" : "" sf.get(i).getInt("id") "" , "value" : "" sf.get(i).getStr("subsidy_type") " -- " sf .get(i).getStr("name") ""},");
}
if(buffer.length()!=0){
renderJson("[" buffer.substring( 0, buffer.length()-1).toString() "]");
}
}else{
renderJson("[{"id":"","value":"- -請選擇--"}]");
}
}
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn