用getElementById获取INPUT标签的值,必须放在BODY后面才能被执行!!
代码如下:
<html> <head> <script language="javascript"> function print(){ var a=document.getElementById("nn").value; alert(a); } </script> </head> <body> <form> <input type="hidden" name="name" id="nn" value="test"/> <input type="button" name="button" value="获取" onclick="print()" /> </form> </body> </html>
DEMO代码本身没有问题,可是我写完后死活获取不到VALUE!!!最后居然发现getElementById必段放在BODY标签后面才能被执行,写好的线上代码:
<input type="hidden" name="field_name" id="flash_time" value="<?php echo get_post_meta('22439','flashtime',true); ?>"> </body> <!-- 秒杀JS --> <script type="text/javascript"> $(document).ready(function () { var oDate = new Date(); var nowTime = oDate.getTime(); //现在的毫秒数 oDate.setDate(oDate.getDate() + 1); // 设定截止时间为第二天 var targetDate = new Date(oDate.toLocaleDateString()); run(targetDate); }); function run(enddate) { getDate(enddate); setInterval("getDate('" + enddate + "')", 500); } function getDate(enddate) { var oDate = new Date(); //获取日期对象 var nowTime = oDate.getTime(); //现在的毫秒数 var enddate = new Date(enddate); //在这里改时间 var targetTime = document.getElementById("flash_time").value;; // 截止时间的毫秒数 var second = Math.floor((targetTime - nowTime) / 1000); //截止时间距离现在的秒数 var day = Math.floor(second / 24 * 60 * 60); //整数部分代表的是天;一天有24*60*60=86400秒 ; second = second % 86400; //余数代表剩下的秒数; var hour = Math.floor(second / 3600); //整数部分代表小时; second %= 3600; //余数代表 剩下的秒数; var minute = Math.floor(second / 60); second %= 60; var spanH = $('.se-txt')[2]; var spanM = $('.se-txt')[1]; var spanS = $('.se-txt')[0]; spanH.innerHTML = tow(hour); spanM.innerHTML = tow(minute); spanS.innerHTML = tow(second); } function tow(n) { return n >= 0 && n < 10 ? '0' + n : '' + n; } </script> <!-- 秒杀ENDS -->
简直就是涨知识啊!!