-
Notifications
You must be signed in to change notification settings - Fork 0
/
简单的表单事件.html
42 lines (37 loc) · 1.04 KB
/
简单的表单事件.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<input id="name" type="text">
<button id="submit-btn">Submit</button>
<script>
// 当点击按钮 submit-btn 时,在console中输出 name 中的内容
var input = document.querySelector('input');
var btn = document.querySelector('button')
btn.addEventListener('click',function(){
var val = input.value;
console.log(val)
})
// 在输入过程中,如果按回车键,则同样执行上一条的需求
input.addEventListener("keydown",function(e){
var val = input.value;
if(e.keyCode==13){
this.blur()
console.log(val)
}
})
//在输入过程中,如果按 ESC 键,则把输入框中的内容清空
input.addEventListener("keydown",function(e){
if(e.keyCode==27){
input.value=''
this.focus()
}
})
</script>
</body>
</html>