HTML
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
<title>联动插件bootstrap-selectN - 光年(Light Year Admin)后台管理系统模板</title>
<link rel="icon" href="favicon.ico" type="image/ico">
<meta name="keywords" content="LightYear,光年,后台模板,后台管理系统,光年HTML模板">
<meta name="description" content="LightYear是一个基于Bootstrap v3.3.7的后台管理系统的HTML模板。">
<meta name="author" content="yinqi">
<link href="http://lyear.itshubao.com/iframe/css/bootstrap.min.css" rel="stylesheet">
<link href="http://example.itshubao.com/demo/css/materialdesignicons.min.css" rel="stylesheet">
<link href="http://lyear.itshubao.com/css/style.min.css" rel="stylesheet">
</head>
<body>
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<div class="card">
<div class="card-header"><h4>联动插件 bootstrap-selectN</h4></div>
<div class="card-body">
<p>该插件是基于layui的一个插件selectN.js,这里去掉了原有layui的东西,根据基本的使用需求做了些修改而改成的一个jquery插件。</p>
<h5>简单使用</h5>
<div class="form-group">
<div id="example-1"></div>
</div>
<pre>var catIns1 = $('#example-1').bootstrapSelectN({
data: catData,
});</pre>
<h5>设置空值项提示</h5>
<div class="form-group">
<div id="example-2"></div>
</div>
<pre>var catIns2 = $('#example-2').bootstrapSelectN({
data: catData,
placeholder: ['设置一', '设置二', '设置三'],
});</pre>
<h5>设置宽度</h5>
<p>可单个使用,也可使用数组形式。<code>['120px', '190px', '200px']</code></p>
<div class="form-group">
<div id="example-3"></div>
</div>
<pre>var catIns3 = $('#example-3').bootstrapSelectN({
data: catData,
width: '190px', // 设置实际长度或者百分比长度,当form表单的class="form-horizontal"时可以使用col-*
});</pre>
<h5>表单中使用</h5>
<form class="form-horizontal">
<div class="form-group">
<label class="col-md-12" for="title">标题</label>
<div class="col-sm-12">
<input type="email" class="form-control" id="title" placeholder="Email">
</div>
</div>
<div class="form-group">
<label class="col-md-12">分类</label>
<div id="example-4"></div>
</div>
</form>
<pre>var catIns4 = $('#example-4').linkageSelect({
data: catData,
width: 'col-md-3',
});</pre>
<h5>url地址获取json数据</h5>
<div class="form-group">
<div id="example-5"></div>
</div>
<pre>var catIns5 = $('#example-5').bootstrapSelectN({
data: 'http://example.itshubao.com/index/test/test_data_selectN_json',
});</pre>
<h5>指定已选中值</h5>
<div class="form-group">
<div id="example-6"></div>
</div>
<pre>var catIns6 = $('#example-6').bootstrapSelectN({
data: catData,
selected: [6,10,11,20,21,22]
});</pre>
<button class="btn btn-default">获取值</button>
<pre id="log" class="m-t-10"></pre>
</div>
</div>
</div>
</div>
</div>
<script type="text/javascript" src="http://lyear.itshubao.com/iframe/js/jquery.min.js"></script>
<script type="text/javascript" src="http://lyear.itshubao.com/iframe/js/bootstrap.min.js"></script>
<script type="text/javascript" src="http://libs.itshubao.com/bootstrap-selectN.min.js"></script>
</body>
</html>
JavaScript
var catData = [{"id":1,"name":"周边旅游","children":[{"id":24,"name":"广东","status":0,"children":[{"id":7,"name":"广州"},{"id":23,"name":"潮州"}]}]},{"id":5,"name":"国内旅游","children":[{"id":8,"name":"华北地区","children":[{"id":9,"name":"北京"}]}]},{"id":6,"name":"出境旅游","children":[{"id":10,"name":"东南亚","children":[{"id":11,"name":"马来西亚","children":[{"id":20,"name":"沙巴","children":[{"id":21,"name":"美人鱼岛","children":[{"id":22,"name":"潜水"}]}]}]}]}]}];
var catIns1 = $('#example-1').bootstrapSelectN({
data: catData,
});
var catIns2 = $('#example-2').bootstrapSelectN({
data: catData,
placeholder: ['设置一', '设置二', '设置三'],
});
var catIns3 = $('#example-3').bootstrapSelectN({
data: catData,
width: '190px', // 设置实际长度或者百分比长度,当form表单的class="form-horizontal"时可以使用col-*
});
var catIns4 = $('#example-4').bootstrapSelectN({
data: catData,
width: 'col-md-3',
});
var catIns5 = $('#example-5').bootstrapSelectN({
data: 'http://example.itshubao.com/index/test/test_data_selectN_json',
placeholder: ['请选择省', '请选择市', '请选择县/区'],
});
var catIns6 = $('#example-6').bootstrapSelectN({
data: catData,
selected: [6,10,11,20,21,22]
});
$('.btn').on('click', function(){
var logbox = $('#log');
logbox.append('catIns1 当前选中的值:', catIns1.values+'\n');
logbox.append('catIns2 当前选中的值:', catIns2.values+'\n');
logbox.append('catIns3 当前选中的值:', catIns3.values+'\n');
logbox.append('catIns4 当前选中的值:', catIns4.values+'\n');
logbox.append('catIns5 当前选中的值:', catIns5.values+'\n');
logbox.append('catIns6 当前选中的值:', catIns6.values+'\n');
});