猿问

这个根据身份证号码输出生日,运行不起来​,是为啥,求高人指点

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>prompt</title>
  <script type="text/javascript">

 function discriCard(){
    var UUserCard = prompt("请输入你的身份证号码");
    var tmpStr= "";
    var birthday= "";
    var strReturn= "";
    alert(0);
    var birthday=document.getElementById("UUserCard ");
            if (UUserCard.length==15)
            {
                tmpStr = birthday.substring(6, 12);
                tmpStr = "19" + tmpStr;
                tmpStr = tmpStr.substring(0, 4) + "-" + tmpStr.substring(4, 6) + "-" + tmpStr.substring(6);
                document.write(tmpStr);
            }
                else {
                tmpStr = birthday.substring(6, 14);
                tmpStr = tmpStr.substring(0, 4) + "-" + tmpStr.substring(4, 6) + "-" + tmpStr.substring(6);
                document.write(tmpStr);}
            }
  </script>
</head>
<body>
    <input name="button" type="button" onClick="discriCard()" value="点击我,输入身份证号!" />
</body>
</html>

菜鸟歌
浏览 1455回答 1
1回答

luofuxiang

    调试了一下楼主的代码,发现有一处存在问题,UUserCard 只是一个变量,而不是某个标签的ID,所以不需要调用getElementByID来获取其值,直接var birthday=UUserCard;即可,代码已经改过调试好了,只改了一行,有注释,楼主可以参考一下。<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>prompt</title>   <script type="text/javascript">  function discriCard(){      var UUserCard = prompt("请输入你的身份证号码");      var tmpStr= "";     var birthday= "";     var strReturn= "";     alert(0);     //var birthday=document.getElementById("UUserCard "); 改为 var birthday=UUserCard;             if (UUserCard.length==15)              {                 tmpStr = birthday.substring(6, 12);                 tmpStr = "19" + tmpStr;                 tmpStr = tmpStr.substring(0, 4) + "-" + tmpStr.substring(4, 6) + "-" + tmpStr.substring(6);                 document.write(tmpStr);             }                 else {                 tmpStr = birthday.substring(6, 14);                 tmpStr = tmpStr.substring(0, 4) + "-" + tmpStr.substring(4, 6) + "-" + tmpStr.substring(6);                 document.write(tmpStr);}             }   </script> </head> <body>     <input name="button" type="button" onClick="discriCard()" value="点击我,输入身份证号!" /> </body> </html>
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答