JavaScript 表单事件

表单事件,当用户与表单元素进行交互时发生。

 

实例:

 1 <!DOCTYPE html>
 2 <html>
 3     <head>
 4         <title>表单事件</title>
 5         <meta charset="UTF-8">
 6         <meta name="viewport" content="width=device-width, initial-scale=1.0">
 7     </head>
 8     <body>  
 9         <form id ="myform" action="http://www.163.com/" method="get" >
10             用户名:<input type="text" id="username" name="username" >
11             <button type="submit">提交</button>
12         </form>
13  
14         <script>
15             var myform = document.getElementById('myform');
16             myform.addEventListener('submit', stop, false);
17             function stop(e) {
18                 var username = document.getElementById('username');
19                 var str = username.value;
20                 if (str === '') {
21                     //要求输入内容
22                     alert("请输入用户名");
23                 } else {
24                     alert("输入的值是:" + str);
25                 }
26             }
27         </script>
28     </body>
29 </html>

 

posted @ 2019-08-02 19:55  梦人亭  阅读(206)  评论(0编辑  收藏  举报