基于jquery的二級聯(lián)動菜單實現(xiàn)代碼
更新時間:2011年04月25日 00:03:34 作者:
基于jquery的二級聯(lián)動菜單實現(xiàn)代碼,腳本之家簡單整理了幾種。需要的朋友可以參考下,主要是思路
jQuery 1.3.2 簡單實現(xiàn)select二級聯(lián)動
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>jQuery 二級聯(lián)動</title>
<script src="http://img.jb51.net/jslib/jquery/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#province").change(function(){
$("#province option").each(function(i,o){
if($(this).attr("selected"))
{
$(".city").hide();
$(".city").eq(i).show();
}
});
});
$("#province").change();
});
</script>
</head>
<body>
<select id="province">
<option>----請選擇省份----
<option>北京
<option>上海
<option>江蘇
</select>
<select class="city">
<option>----請選擇城市----
</select>
<select class="city">
<option>東城</option>
<option>西城</option>
<option>崇文</option>
<option>宣武</option>
<option>朝陽</option>
</select>
<select class="city">
<option>黃浦</option>
<option>盧灣</option>
<option>徐匯</option>
<option>長寧</option>
<option>靜安</option>
</select>
<select class="city">
<option>南京</option>
<option>鎮(zhèn)江</option>
<option>蘇州</option>
<option>南通</option>
<option>揚州</option>
</select>
</body>
</html>
JQuery實現(xiàn)的二級聯(lián)動菜單
先看頁面代碼
Html代碼
<tr>
<td align="right" width="30%"><span class="red">*</span>短信類型:</td>
<td align="left">
<select name='city' id='first'>
<option value='-1'>==請選擇類型==</option>
<#list typeList as t>
<option value='${t.id}'>${t.name}</option>
</#list>
</select>
<span id="second">
<select id="area" name="msgTypeId">
</select>
</span>
</td>
</tr>
其中id為first的下拉列表為第一個下拉列表,id為second的區(qū)域為第二個下拉列表。
JavaScript代碼:
<script language="javascript">
$(function(){
$("#second").hide(); //初始化的時候第二個下拉列表隱藏
$("#first").change(function(){ //當?shù)谝粋€下拉列表變動內(nèi)容時第二個下拉列表將會顯示
var parentId=$("#first").val();
if(null!= parentId && ""!=parentId){
$.getJSON("http://localhost/msg/getSecondTypesJson",{id:parentId},function(myJSON){
var options="";
if(myJSON.length>0){
options+="<option value=''>==請選擇類型==</option>";
for(var i=0;i<myJSON.length;i++){
options+="<option value="+myJSON[i].id+">"+myJSON[i].name+"</option>";
}
$("#area").html(options);
$("#second").show();
}
else if(myJSON.length<=0){
$("#second").hide();
}
});
}
else{
$("#second").hide();
}
});
});
</script>
復制代碼 代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>jQuery 二級聯(lián)動</title>
<script src="http://img.jb51.net/jslib/jquery/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#province").change(function(){
$("#province option").each(function(i,o){
if($(this).attr("selected"))
{
$(".city").hide();
$(".city").eq(i).show();
}
});
});
$("#province").change();
});
</script>
</head>
<body>
<select id="province">
<option>----請選擇省份----
<option>北京
<option>上海
<option>江蘇
</select>
<select class="city">
<option>----請選擇城市----
</select>
<select class="city">
<option>東城</option>
<option>西城</option>
<option>崇文</option>
<option>宣武</option>
<option>朝陽</option>
</select>
<select class="city">
<option>黃浦</option>
<option>盧灣</option>
<option>徐匯</option>
<option>長寧</option>
<option>靜安</option>
</select>
<select class="city">
<option>南京</option>
<option>鎮(zhèn)江</option>
<option>蘇州</option>
<option>南通</option>
<option>揚州</option>
</select>
</body>
</html>
JQuery實現(xiàn)的二級聯(lián)動菜單
先看頁面代碼
Html代碼
復制代碼 代碼如下:
<tr>
<td align="right" width="30%"><span class="red">*</span>短信類型:</td>
<td align="left">
<select name='city' id='first'>
<option value='-1'>==請選擇類型==</option>
<#list typeList as t>
<option value='${t.id}'>${t.name}</option>
</#list>
</select>
<span id="second">
<select id="area" name="msgTypeId">
</select>
</span>
</td>
</tr>
其中id為first的下拉列表為第一個下拉列表,id為second的區(qū)域為第二個下拉列表。
JavaScript代碼:
復制代碼 代碼如下:
<script language="javascript">
$(function(){
$("#second").hide(); //初始化的時候第二個下拉列表隱藏
$("#first").change(function(){ //當?shù)谝粋€下拉列表變動內(nèi)容時第二個下拉列表將會顯示
var parentId=$("#first").val();
if(null!= parentId && ""!=parentId){
$.getJSON("http://localhost/msg/getSecondTypesJson",{id:parentId},function(myJSON){
var options="";
if(myJSON.length>0){
options+="<option value=''>==請選擇類型==</option>";
for(var i=0;i<myJSON.length;i++){
options+="<option value="+myJSON[i].id+">"+myJSON[i].name+"</option>";
}
$("#area").html(options);
$("#second").show();
}
else if(myJSON.length<=0){
$("#second").hide();
}
});
}
else{
$("#second").hide();
}
});
});
</script>
相關(guān)文章
詳解jquery插件jquery.viewport.js學習使用方法
本篇文章主要介紹了詳解jquery插件jquery.viewport.js學習使用方法,具有一定的參考價值,有興趣的可以了解一下2017-09-09jQuery插件datalist實現(xiàn)很好看的input下拉列表
本文給大家分享的是使用jQuery實現(xiàn)的房HTML5中的一個好看的input框很好看的下拉列表--datalist,兼容性非常不錯,這里推薦給大家,有需要的小伙伴可以參考下。2015-07-07DataTables+BootStrap組合使用Ajax來獲取數(shù)據(jù)并且動態(tài)加載dom的方法(排序,過濾,分頁等)
Datatables是一款jquery表格插件。它是一個高度靈活的工具,可以將任何HTML表格添加高級的交互功能。這篇文章主要介紹了DataTables+BootStrap組合使用Ajax來獲取數(shù)據(jù)并且動態(tài)加載dom的方法(排序,過濾,分頁等),需要的朋友可以參考下2016-11-11JQuery Tips(3) 關(guān)于$()包裝集內(nèi)元素的改變
JQuery包裝集內(nèi)的元素在一開始的選定后,還可以通過一系列JQuery提供的方法對包裝集內(nèi)的元素進行擴充,修改,篩選,刪除find()方法 VS filter()方法2009-12-12