-
Notifications
You must be signed in to change notification settings - Fork 1
/
lcCascade-select.html
114 lines (102 loc) · 3.41 KB
/
lcCascade-select.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
body{
position: relative;
}
div.cascader{
position: relative;
display: inline;
/*background: red*/
}
div.selectedL{
position: absolute;
left: 20%;
display: inline-block;
}
/* i{
display: block;
}*/
</style>
</head>
<body>
<div class="cascader"></div>
<button id="commit">提交</button>
<div class="selectedL"><h2>选择过的地址</h2></div>
<script type="text/javascript" src="location.js"></script>
<script>
var llocation = llocation;
var defaultLocation = ['广东省','广州市','天河区'];
// var button = document.getElementById('commit');
var selectDiv = document.querySelector('div.selectedL');
// var lcc = new LcCascader('div.cascader',llocation,defaultLocation);
var option = {
lcList: 'div.cascader',
data: llocation,
defaultLocation: defaultLocation
}
new LcCascader(option);
function LcCascader(option) {
var _this = this;
this.data = option.data;
//要注意把nodelist换成节点
this.div = document.querySelectorAll(option.lcList)[0];
this.selectList = this.div.getElementsByTagName('select');
this.lastLocation = [];
this.init = function(){
//添加一个默认参数调用函数,当发生点击事件时,把默认参数去掉再调用
this.selectLocation(0,this.data,option.defaultLocation);
// this.selectLocation(0,data);
}
this.selectLocation = function(selectIndex,data,defaultLocation){
var _this = this
if(!data||data.length == 0){
; return
}
while(this.selectList.length > selectIndex){
this.div.removeChild(this.selectList[this.selectList.length - 1]);
}
var selectP = document.createElement('select');
selectP.add(new Option('请选择'));
selectP.options[0].style.display='none';
// selectP.selectedIndex = -1;
this.div.appendChild(selectP);
selectP.addEventListener('change',function(){
if(selectIndex == 0){
_this.lastLocation = [];
};
//去掉默认参数再调用
_this.lastLocation.splice(selectIndex,1,data[selectP.selectedIndex - 1].name);
_this.selectLocation(selectIndex + 1,data[selectP.selectedIndex - 1].children);
})
for(let i = 0; i < data.length; i++){
selectP.add(new Option(data[i].name,i));
if(defaultLocation != undefined){
if(data[i].name == defaultLocation[selectIndex]){
selectP.selectedIndex = i+1;
this.lastLocation.splice(selectIndex,1,data[i].name);
this.selectLocation(selectIndex + 1, data[i].children,defaultLocation);
}
}
}
}
this.getLastLocation = function(){
return this.lastLocation;
}
this.init();
}
button.onclick = function(){
var lastLocation = lcc.getLastLocation();
if(confirm("确认选择:" + lastLocation.join('') + "?")){
var i = document.createElement('i');
var time = new Date();
i.innerText = time.toLocaleTimeString() + '---' + lastLocation.join('');
selectDiv.appendChild(i);
}
}
</script>
</body>
</html>