若依添加省市縣聯動三級菜單


1:下載三級菜單源碼

  源碼地址

先下載如上源碼,看運行效果


2:把數據放入到spring boot static 文件下

 

 

 

2:添加頁面add.html相關操作

A )thymeleaf include引入公共頁面 js /css

 

 B)在add.html添加菜單代碼

 

 

 在上圖指定位置,插入如下javascript代碼

<script type="text/javascript">
	$(function () {
		var html = "<option value=''>== 請選擇 ==</option>"; $("#input_city").append(html); $("#input_area").append(html);
		$.each(pdata,function(idx,item){
			if (parseInt(item.level) == 0) {
   				html += "<option value='" + item.names + "' exid='" + item.code + "'>" + item.names + "</option>";
   			}
		});
		$("#input_province").append(html);

		$("#input_province").change(function(){
			if ($(this).val() == "") return;
			$("#input_city option").remove(); $("#input_area option").remove();
			var code = $(this).find("option:selected").attr("exid"); code = code.substring(0,2);
			var html = "<option value=''>== 請選擇 ==</option>"; $("#input_area").append(html);
			$.each(pdata,function(idx,item){
				if (parseInt(item.level) == 1 && code == item.code.substring(0,2)) {
	   				html += "<option value='" + item.names + "' exid='" + item.code + "'>" + item.names + "</option>";
	   			}
			});
			$("#input_city").append(html);		
		});

		$("#input_city").change(function(){
			if ($(this).val() == "") return;
			$("#input_area option").remove();
			var code = $(this).find("option:selected").attr("exid"); code = code.substring(0,4);
			var html = "<option value=''>== 請選擇 ==</option>";
			$.each(pdata,function(idx,item){
				if (parseInt(item.level) == 2 && code == item.code.substring(0,4)) {
	   				html += "<option value='" + item.names + "' exid='" + item.code + "'>" + item.names + "</option>";
	   			}
			});
			$("#input_area").append(html);		
		});
		//綁定
		$("#input_province").val("廣東省");$("#input_province").change();
		$("#input_city").val("深圳市");$("#input_city").change();
		$("#input_area").val("羅湖區");

	});
</script>

C)在add.html所需要省市縣位置添加如下代碼

<div class="row">
    <div class="col-sm-12">
        <div class="form-group">
            <label class="control-label col-sm-2">所在區域</label>
            <div class="col-sm-3">
                <select name="input_province" id="input_province" class="form-control">
                </select>
            </div>
            <div class="col-sm-3">
                <select name="input_city" id="input_city" class="form-control">
                </select>
            </div>
            <div class="col-sm-3">
                <select name="input_area" id="input_area" class="form-control">
                </select>
            </div>
        </div>
    </div>
</div>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM