text 内容修改事件

 1 <script type="text/javascript">
 2             window.onload = function() {
 3                 
 4                 //取到选项卡
 5                 var idtype = document.getElementById("selPassType");
 6                 
 7                 //当选项卡发生改变时,触发事件
 8                  idtype.onchange =function(){
 9                      
10                      //判断是否是目标选项
11                      if(idtype.value == "01"){
12                          //取到身份证输入框
13                          var txtid = document.getElementById("txtPassCode");
14                          //取到出生日期输入框,这里无用
15                         var txtbir = document.getElementById("dpBirthDay");
16                         
17                         
18 //监控txet的有三种方法
19 //onchange 是当内容发生变化,并且失去焦点的时候触发,限制比较多
20 //onpropertychange  实时触发,即每有一个字符发生变化都会触发,通过js引起的内容变化也会触发事件,单仅限于IE
21 //oninput 也是实时触发的事件,浏览器兼容性较好,并且JS引起的变化不会触发事件
22 //但是 有文章说  object.addEventListener("input",fn,false);  
23                         
24                         //判断浏览器类型
25                          if(document.all){
26                              txtid.onpropertychange = function(){
27                                  GetBirth();
28                              }
29                          }
30                          else{
31                              txtid.oninput = function(){
32                                  GetBirth();
33                              }
34                          }
35                      }
36                  }                
37             }
38             
39             //函数,获取身份证中的生日部分
40             function GetBirth() {
41                 var txtid = document.getElementById("txtPassCode");
42                 var txtbir = document.getElementById("dpBirthDay");
43                 
44                 //身份证的长度为15位(第一代身份证) 和 18位(第二代身份证)
45                 if(txtid.value.length == 15 || txtid.value.length == 18){
46                     var bithday="";
47                     if(txtid.value.length == 15){
48                         //第一代身份证的出生日期在7-12位
49                          bithday = "19"+txtid.value.substring(6,12);
50                     }
51                     else{
52                         txtbir.value = "";
53                         //第二代身份证的出生日期在7到14位
54                         bithday = txtid.value.substring(6,14);
55                     }
56                     txtbir.value = bithday.substring(0,4)+"-"+bithday.substring(4,6)+"-"+bithday.substring(6,8);
57                 }
58             }
59         </script>
View Code

利用js 取到省份证的出生日期,并插入, 主要留意 关于text的变化的事件控制

posted @ 2017-06-28 13:19  黑二胖  阅读(342)  评论(0编辑  收藏  举报