jquery结合bootstrap的选择框联动
2016-04-06 17:54
555 查看
1、配置bootstrap环境
2、调用jquery操作组件,利用bootstrap的JavaScript插件
3、页面1-liandong.html;页面2-liandong.js
页面1-liandong.html;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>New Web Project</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
<script src="js/jquery-1.12.3.js"></script>
<script type="text/javascript" src="liandong.js"></script>
</head>
<body>
<!-- Single button -->
<div class="btn-group">
<button type="button" id="pro" class="btn btn-info dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
省份<span class="caret"></span>
</button>
<ul class="dropdown-menu" id="proList" >
</ul>
</div>
<!-- Single button -->
<div class="btn-group">
<button type="button" id="city" class="btn btn-info dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
城市 <span class="caret"></span>
</button>
<ul class="dropdown-menu" id="cityList" >
</ul>
</div>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
页面2-liandong.js
var sheng_json=[
{sheng:'湖北',shi:['wuhan','huanggang','jingzhou']},
{sheng:'湖南',shi:['wuhan1','huanggang1','jingzhou1']},
{sheng:'广州',shi:['wuhan2','huanggang2','jingzhou2']},
];
var sheng_length = sheng_json.length;
$(function(){
var flage = $("#proList").children().get(0);
if(flage == null){
for(var i=0;i<sheng_length;i++){
var sheng = sheng_json[i].sheng;
$("#proList").append("<li><a href=\"#\" value="+sheng+">"+sheng+"</a></li>");
}
}
$('#proList li a').on('click', function(event) {
var sheng = $(this).attr('value');
var citys = new Array();
$("#pro").html(sheng);
for(var i=0;i<sheng_length;i++){
if(sheng == sheng_json[i].sheng){
citys = sheng_json[i].shi;
break;
}
}
var flage2 = $("#cityList").children().get(0);
if(flage2 != null){
$("#cityList").empty();
$("#city").html("城市");
}
for(var j=0;j<citys.length;j++){
$("#cityList").append("<li><a href=\"#\" value="+citys[j]+">"+citys[j]+"</a></li>");
}
});
$("#city").on('click', function(event) {
$("#cityList li a").on('click', function(event) {
var city = $(this).attr('value');
$("#city").html(city);
});;
});
});
2、调用jquery操作组件,利用bootstrap的JavaScript插件
3、页面1-liandong.html;页面2-liandong.js
页面1-liandong.html;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>New Web Project</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
<script src="js/jquery-1.12.3.js"></script>
<script type="text/javascript" src="liandong.js"></script>
</head>
<body>
<!-- Single button -->
<div class="btn-group">
<button type="button" id="pro" class="btn btn-info dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
省份<span class="caret"></span>
</button>
<ul class="dropdown-menu" id="proList" >
</ul>
</div>
<!-- Single button -->
<div class="btn-group">
<button type="button" id="city" class="btn btn-info dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
城市 <span class="caret"></span>
</button>
<ul class="dropdown-menu" id="cityList" >
</ul>
</div>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
页面2-liandong.js
var sheng_json=[
{sheng:'湖北',shi:['wuhan','huanggang','jingzhou']},
{sheng:'湖南',shi:['wuhan1','huanggang1','jingzhou1']},
{sheng:'广州',shi:['wuhan2','huanggang2','jingzhou2']},
];
var sheng_length = sheng_json.length;
$(function(){
var flage = $("#proList").children().get(0);
if(flage == null){
for(var i=0;i<sheng_length;i++){
var sheng = sheng_json[i].sheng;
$("#proList").append("<li><a href=\"#\" value="+sheng+">"+sheng+"</a></li>");
}
}
$('#proList li a').on('click', function(event) {
var sheng = $(this).attr('value');
var citys = new Array();
$("#pro").html(sheng);
for(var i=0;i<sheng_length;i++){
if(sheng == sheng_json[i].sheng){
citys = sheng_json[i].shi;
break;
}
}
var flage2 = $("#cityList").children().get(0);
if(flage2 != null){
$("#cityList").empty();
$("#city").html("城市");
}
for(var j=0;j<citys.length;j++){
$("#cityList").append("<li><a href=\"#\" value="+citys[j]+">"+citys[j]+"</a></li>");
}
});
$("#city").on('click', function(event) {
$("#cityList li a").on('click', function(event) {
var city = $(this).attr('value');
$("#city").html(city);
});;
});
});
相关文章推荐
- JQuery1——基础($对象,选择器,对象转换)
- Android学习笔记(二九):嵌入浏览器
- Android java 与 javascript互访(相互调用)的方法例子
- bootstrap初试进度条
- JavaScript演示排序算法
- 2015-2016网页设计趋势分析 Web Design of Trends
- javascript实现10进制转为N进制数
- jQuery Ajax 跨域调用
- Bootstrap 3.3.4 发布,Web 前端 UI 框架
- 移动端的长按事件
- jquery教程靠边站,一分钱不花让你免费学会jquery
- JQuery+Strusts1.x无刷新登录
- java自动生成验证码插件-kaptcha
- 最后一次说说闭包
- Ajax
- $.ajax()方法详解
- jQuery ajax - ajax() 方法
- 2019年开发人员应该学习的8个JavaScript框架