-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.html
91 lines (81 loc) · 2.54 KB
/
test.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
<html>
<head>
<meta charset="utf-8"/>
</head>
<script>
var obj = {};
function ajax(url, param, callback) {
var xhr = null;
if (obj.hasOwnProperty('xhr') && obj['xhr'] instanceof XMLHttpRequest) {
xhr = obj['xhr'];
} else {
xhr = obj['xhr'] = new XMLHttpRequest();
}
xhr.onreadystatechange = function () {
// console.log(xhr.responseText);
if (xhr.readyState == 4 && xhr.status == 200) {
callback(xhr.responseText);
}
}
xhr.open('POST', url, true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.send(param);
}
function $(elem) {
return document.querySelector(elem);
}
function $$(elem) {
return document.querySelectorAll(elem);
}
function repeat(char, num){
var str = '';
for (var i = 0; i < num; i++){
str += char;
}
return str;
}
function del(id){
ajax('http://localhost/test.php?del=1', 'id='+id, function(data){
window.location.reload();
});
}
function getAllCategory() {
ajax('http://localhost/test.php?category=1', '', function (data) {
var d = JSON.parse(data);
var html = '<option value="1" >select</option>';
var li = '';
for (var i = 0; i < d.length; i++) {
html += '<option value="' + d[i].id + '">' + d[i].name + '</option>';
li += '<li>'+ repeat(' ', (~~d[i].level - 1)) + d[i].name + '<button onclick="del(' + d[i].id + ')" >del</button></li>';
}
$('#category').innerHTML = html;
$('#list').innerHTML = li;
})
}
window.onload = function () {
getAllCategory();
$('#submit').addEventListener('click', function () {
var pid = 1;
$$('#category option').forEach(function (elem, i, allElem) {
if (elem.selected) {
pid = elem.value;
}
});
var name = $('#name').value;
var param = 'name='+name+'&pid='+pid;
ajax('http://localhost/test.php?save=1', param, function (data) {
window.location.reload();
})
}, false);
}
</script>
<body>
<p>上级分类</p>
<select id="category" title="">
</select>
<input type="text" id="name" title=""/>
<input type="button" id="submit" value="submit"/>
<ul id="list">
</ul>
</body>
</html>