JavaScript form events

Form events occur when a user interacts with the form elements.

 

Example:

 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 is                  IF (STR === '' ) {
 21 is                      // required input content 
22 is                      Alert ( " Please enter the user name " );
 23                  } the else {
 24                      Alert ( " value entered is: " + STR);
 25                  }
 26 is              }
 27          </ Script>
 28      </ body>
29 </html>

 

Guess you like

Origin www.cnblogs.com/q2546/p/11290904.html