<!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>
1 回答
已采纳
luofuxiang
TA贡献34条经验 获得超34个赞
调试了一下楼主的代码,发现有一处存在问题,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>
添加回答
举报
0/150
提交
取消