首页 > 自执行js,为什么下面的js获取checkbox的值不执行呢。

自执行js,为什么下面的js获取checkbox的值不执行呢。

<html>
    <head>
        <script type="text/javascript">

            (function() { 
                alert("here.....");
                var ss = document.getElementById("cc").value;
                alert("xxxxx= "+ss);
            })()
        </script>
    </head>

    <body>
        <input type="checkbox" id = "cc" name="c" onclick="getit();" onmouseover="mouseover();" disabled>
    </body>

<html>

因为DOM没有加载完成, 无法读取 value .

解决方法:

DEMO:

<html>
<head>
  <script>
    alert("here.....");
    window.onload = function() { 
      var a = document.getElementById("cc").value;
      alert("xxxxx= "+a);
    }
  </script>
</head>
<body>
  <input type="checkbox" id="cc">
</body>
<html>
【热门文章】
【热门文章】