jquery怎樣實現ajax聯動框(一)

來源:互聯網
上載者:User

前台頁面 複製代碼 代碼如下:<script type="text/javascript" src="${rc.contextPath}/js/jquery.select.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#rwflSelect").linkSelect({
nodata:"none",
required:true,
firstUrl:'${rc.contextPath}/repair/loadCategory',
secondUrl:'${rc.contextPath}/repair/loadSubCategory',
firstValue:'$!{repair.categoryid}',//任務大類
secondValue:'$!{repair.subcategoryid}'//人物小類
});
});
</script>
<tr id="rwflSelect">
<td width="100" class="t_r prten field_c">任務分類:</td>
<td width="131"><select class="first" name='categoryid'></select> </td>
<td width="10"></td>
<td width="131"><select class="second" name="subcategoryid" disabled="disabled"></select></td>
</tr>


jquery.select.js 複製代碼 代碼如下:/*
Ajax 三級聯動
日期:2013-2-26
settings 參數說明
-----
firstUrl:一級下拉資料擷取URL,josn返回
firstValue:預設一級下拉value
secondUrl:二級下拉資料擷取URL,josn返回
secondValue:預設二級下拉value
thirdUrl:三級下拉資料擷取URL,josn返回
thirdValue:預設三級下拉value
nodata:無資料狀態
required:必選項
------------------------------ */
(function($){
$.fn.linkSelect=function(settings){
if($(this).size()<1){return;};
// 預設值
settings=$.extend({
firstUrl:"js/city.min.js",
firstValue:null,
secondValue:null,
thirdValue:null,
nodata:null,
required:true
},settings);
var box_obj=this;
var first_obj=box_obj.find(".first");
var second_obj=box_obj.find(".second");
var third_obj=box_obj.find(".third");
var select_prehtml=(settings.required) ? "" : "<option value=''>請選擇</option>";
var prepareSelectHtml=function(jsonArray){
var temp_html=select_prehtml;
$.each(jsonArray,function(index,row){
temp_html+="<option value='"+row.value+"'>"+row.text+"</option>";
});
return temp_html;
};
// 賦值二級下拉框函數
var secondStart=function(){
second_obj.empty().attr("disabled",true);
third_obj.empty().attr("disabled",true);
if(first_obj.val()==''){
return;
}
$.getJSON(settings.secondUrl, { firstValue: first_obj.val(), time: new Date().getTime() }, function(jsonResult){
if(!jsonResult.success){
if(settings.nodata=="none"){
second_obj.css("display","none");
third_obj.css("display","none");
}else if(settings.nodata=="hidden"){
second_obj.css("visibility","hidden");
third_obj.css("visibility","hidden");
};
return;
}
// 遍曆賦值二級下拉式清單
second_obj.html(prepareSelectHtml(jsonResult.data)).attr("disabled",false).css({"display":"","visibility":""});
thirdStart();
});

};
// 賦值三級下拉框函數
var thirdStart=function(){
third_obj.empty().attr("disabled",true);
$.getJSON(settings.thirdUrl, { firstValue: first_obj.val(),secondValue:second_obj.val(), time: new Date().getTime() }, function(jsonResult){
if(!jsonResult.success){
if(settings.nodata=="none"){
third_obj.css("display","none");
}else if(settings.nodata=="hidden"){
third_obj.css("visibility","hidden");
};
return;
}
// 遍曆賦值三級下拉式清單
third_obj.html(prepareSelectHtml(jsonResult.data)).attr("disabled",false).css({"display":"","visibility":""});
thirdStart();
});
};
var init=function(){
// 遍曆賦值一級下拉式清單
$.getJSON(settings.firstUrl, {time: new Date().getTime() }, function(jsonResult){
if(!jsonResult.success){
return;
}
// 遍曆賦值一級下拉式清單
first_obj.html(prepareSelectHtml(jsonResult.data));
secondStart();
// 若有傳入一級與二級的值,則選中。(setTimeout為相容IE6而設定)
setTimeout(function(){
if(settings.firstValue && settings.firstValue.length>0){
first_obj.val(settings.firstValue);
secondStart();
setTimeout(function(){
if(settings.secondValue && settings.secondValue.length>0){
second_obj.val(settings.secondValue);
thirdStart();
setTimeout(function(){
if(settings.thirdValue && settings.thirdValue.length>0){
third_obj.val(settings.thirdValue);
};
},1);
};
},1);
};
},1);
});
// 選擇一級時發生事件
first_obj.bind("change",function(){
secondStart();
});
// 選擇二級時發生事件
second_obj.bind("change",function(){
thirdStart();
});
};
// 初始化第一個下拉框
init();
};
})(jQuery);

${rc.contextPath}/repair/loadCategory 對應的後台方法及返回json值: 複製代碼 代碼如下:@RequestMapping("loadCategory")
@ResponseBody
public Map<String, Object> loadCategory(ModelMap model){
String msg = "";
boolean isSuccess = false;
List<Map<String, String>> maps=new ArrayList<Map<String,String>>();
try {
List<Category> categories= categoryService.findAllCategory();
for (Category category : categories) {
Map<String,String> map=new HashMap<String, String>();
map.put("value", category.getId().toString());
map.put("text", category.getCategoryName());
maps.add(map);
}
msg = "尋找大類成功。";
isSuccess=true;
} catch (Exception e) {
msg = "尋找大類失敗。";
log.error("尋找大類失敗:" + e.getMessage(), e);
}
return buildAjaxResult(isSuccess, msg,maps);
}
protected Map<String, Object> buildAjaxResult(boolean isSuccess, String msg, Object data) {
Map<String, Object> resultMap = new HashMap<String, Object>();
resultMap.put("success", isSuccess);
resultMap.put("msg", msg);
resultMap.put("data", data);
return resultMap;
}

效果

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.