多级联动的select框
JS文件:
; (function($, w) {
var LinkSelect = function(config) {
var opt = {
doms: config.doms || [],
url: config.url || '',
type: config.type || 'get',
data: config.data || []
};
if (opt.doms.length < ) {
return console.log('必须两个或两个以上下拉框');
}
function initial() {
for (var i = ; i < opt.doms.length; i++) {
(function(idx) {
opt.doms[idx].on('change',
function () {
var val = $(this).val();
var temp = getDataByValue(idx);
var nextDom = opt.doms[idx + ];
if (nextDom && nextDom.length > ) {
setDomData(nextDom, temp);
}
});
})(i);
}
if (opt.data && opt.data.length > ) {
initialUi();
} else {
if (!opt.url || opt.url === '') {
return console.log('配置无效,必须指定url或data');
} else {
$.ajax({
url: opt.url,
type: opt.type,
success: function(response) {
opt.data = response;
initialUi();
}
});
}
}
}
function initialUi() {
var dom = opt.doms[];
setDomData(dom, opt.data);
}
function getDataByValue(idx) {
var source = opt.data;
for (var i = ; i <= idx; i++) {
(function(idx) {
var temp = source.find(function(item) {
return item.value.toString() === opt.doms[idx].val();
});
if (temp && temp.children) {
source = temp.children;
} else {
source = [];
}
})(i);
}
return source;
}
function resetDom(dom) {
dom.html('<option value="">请选择</option>');
}
function setDomData(dom, data) {
if (!data || data.length === ) {
resetDom(dom);
} else {
var html = '<option value="">请选择</option>';
$.each(data,
function(idx, item) {
html += '<option value="' + item.value + '">' + item.text + '</option>';
});
dom.html(html);
}
dom.trigger('change');
}
initial();
};
w.LinkSelect = LinkSelect;
})(jQuery, window);
页面结构
<div class="container">
<h2>Index</h2>
<hr />
<div class="form-box">
<div class="row">
<div class="label">年级</div>
<div class="control">
<select id="grade"></select>
</div>
</div>
<div class="row">
<div class="label">班级</div>
<div class="control">
<select id="clazz"></select>
</div>
</div>
<div class="row">
<div class="label">组</div>
<div class="control">
<select id="group"></select>
</div>
</div>
</div>
</div>
使用方法:
<script src="lib/myui/LinkSelect.js"></script>
<script>
var ls = new LinkSelect({
doms: [$('#grade'), $('#clazz'), $('#group')],
data: [
{
text: '苗班',
value: ,
children: [
{
text: '苗1班',
value: ,
children: [
{
text: '1组',
value: ,
children: []
}]
},
{
text: '苗2班',
value: ,
children: []
}]
},
{
text: '小班',
value: ,
children: [
{
text: '小1班',
value: ,
children: []
}]
},
{
text: '中班',
value: ,
children: [
{
text: '中1班',
value: ,
children: []
},
{
text: '中2班',
value: ,
children: []
}]
}]
})
</script>
多级联动的select框的更多相关文章
- JQuery Easyui/TopJUI 创建多级联动下拉框(纯HTML实现!!!)
JQuery Easyui/TopJUI 创建多级联动下拉框(纯HTML实现!!!) 效果展示: 代码如下: <form data-toggle="topjui-form"& ...
- ajax 多级联动 下拉框 Demo
写了ajax实现级联下拉框,考虑常用,并且级联个数随不同业务个数不同,于是就整理了一下,实现了 ajax + N级联动 下拉框的效果 效果图 HTML 代码 <h2> 省级联动</h ...
- bootstrap 多级联动下拉框
<!DOCTYPE HTML> <html lang="zh-CN"> <head> <meta charset="UTF-8& ...
- jQuery制作简洁的多级联动Select下拉框
今天我们要来分享一款很实用的jQuery插件,它是一个基于jQuery多级联动的省市地区Select下拉框,并且值得一提的是,这款联动下拉框是经过自定义美化过的,外观比浏览器自带的要漂亮许多.另外,这 ...
- vue 获取数据联动下拉框select ,并解决报Duplicate value found in v-for="...": "". Use track-by="$index" 错误
公司项目中遇到一个问题,联动下拉框,并且数据是使用vue-resource从后台获取的,格式不利于输出联动下拉框,联动下拉框是第一个下拉框输出一个数组里每一项json的一个text值,从而第二下拉框输 ...
- MVC实现多级联动
前言 多级联动(省级联动)的效果,网上现成的都有很多,各种JS实现,Jquery实现等等,今天我们要讲的是在MVC里面,如何更方便.更轻量的实现省级联动呢? 实现效果如下: 具体实现 如图所示,在HT ...
- 使用chosen插件实现多级联动和置位
使用chosen插件实现多级联动和置位 首先写好第一个select,加上onchage属性之后,写onchange方法. <select data-placeholder="选择省份. ...
- C/C++ Qt 数据库与ComBox多级联动
Qt中的SQL数据库组件可以与ComBox组件形成多级联动效果,在日常开发中多级联动效果应用非常广泛,例如当我们选择指定用户时,我们让其在另一个ComBox组件中列举出该用户所维护的主机列表,又或者当 ...
- PHP多级联动的学习(一)
我尝试在ThinkCMF中实现多级联动,首先我开始看了dede的联动类别管理前后台的代码以及他的数据库,经过非常多次的尝试,我渐渐有了一点想法,并给予实施. 首先写出前台的界面.如图. 然后在数据库中 ...
随机推荐
- VC++6.0注释快捷键的添加使用
在eclipse等编辑工具中都有注释代码的快捷键,但是vc++6.0没有. vc++是以VB为脚本来控制的,在网上找到了一个VB的脚本供大家使用. 工具/原料 VC++6.0 方法/步骤 打开VC ...
- Operating System-Thread(4) 线程的调度激活机制(Scheduler Activations)
本文主要介绍线程的调度激活机制(Scheduler Activations),主要内容: 调度激活机制简介 上行调用(upcall) 中断处理(Interrupt) 一. 调度激活机制简介 上一篇文章 ...
- mac下安装node
学着使用homebrew进行安装,发现很是方便. homebrew是mac下的一款管理安装的工具. 1. 安装homebrew 使用mac自带的ruby下载安装: ruby -e "$(cu ...
- wpf dataGrid 选中行 失去焦点时 的背景颜色的更改
关于 wpf dataGrid 选中行 失去焦点时 的背景颜色的更改.很简单的方式,在datagrid的resource中更改InactiveSelectionHighlightBrushKey属性的 ...
- Java探索之旅(1)——概述与控制台输入
使用的课本: Java语言程序设计(基础篇)----西电 李娜(译) 原著: Introduction to Java Progrmming(Eighth Edition) -----Y.Daniel ...
- Javascript作用域和变量提升
下面的程序是什么结果? var foo = 1; function bar() { if (!foo) { var foo = 10; } alert(foo); } bar(); 结果是10: 那么 ...
- ubuntu server usb安装盘制作问题
本来服务器上装的是windows server 2003,开多个虚拟机装linux来用,但发现不管是vmware还是virtualbox,总是有内存泄漏,大约2个星期左右16G内存就全没了,任务管理器 ...
- Entity Framework Code-First(9.8):DataAnnotations - Column Attribute
DataAnnotations - Column Attribute: Column attribute can be applied to properties of a class. Defaul ...
- Spring入门第六课
XML配置里的Bean自动装配 Spring IOC容器可以自动装配Bean.需要做的仅仅是在<bean>的autowire属性里指定自动装配的模式 ByType(根据类型自动装配):若I ...
- Windows使用Github
首先,你需要执行下面两条命令,作为 git 的基础配置,作用是告诉 git 你是谁,你输入的信息将出现在你创建的提交中. git config --global user.name "你的名 ...