-
Notifications
You must be signed in to change notification settings - Fork 1
/
13-表单属性操作-input赋值和取值.html
57 lines (47 loc) · 1.12 KB
/
13-表单属性操作-input赋值和取值.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
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
input {
display: block;
margin: 10px 0;
}
</style>
</head>
<body>
<input type="text"/>
<input type="text"/>
<input type="text"/>
<input type="text"/>
<input type="text"/>
<input type="text"/>
<input type="text"/>
<input type="text"/>
<button>赋值</button>
<button>获取</button>
<script>
//需求:点击按钮,为每一个input赋值,和获取所有值。
var inpArr = document.getElementsByTagName("input");
var btnArr = document.getElementsByTagName("button");
//赋值
btnArr[0].onclick = function () {
//利用for循环,为每一个input赋值。
for(var i=0;i<inpArr.length;i++){
inpArr[i].value = i;
}
}
//获取
btnArr[1].onclick = function () {
var str = "";
//利用for循环,为每一个input赋值。
for(var i=0;i<inpArr.length;i++){
//累加到一个string中。
str += inpArr[i].value;
}
console.log(str);
}
</script>
</body>
</html>