document.getElementById().onclick 有问题

trouble with document.getElementById().onclick

我需要创建一个 T&C 的初始页面,用户可以在其中勾选方框,然后提交按钮启用并将它们转发到正确的页面。

目前我不明白为什么以下代码使条款复选框处于活动状态 link - 单击它以选中该框从 window.open

加载页面

如果我注释掉 window.open 两行,它会按预期工作,但没有 link 功能。显然,我正在尝试创建一个适用于所有移动和桌面客户端的页面,否则我会在按钮 html.

中使用内联 onclick

感谢您的帮助...

<script>
 function disableSubmit() {
  document.getElementById("submit").disabled = true;
 }

  function activateBox(element) {

      if(element.checked) {
        document.getElementById("submit").disabled = false;
  document.getElementById("submit").value = "Press here to go online";
  document.getElementById("submit").onclick= window.open('<?php print $grant_url ?>');
  document.getElementById("submit").ontouchstart= window.open('<?php print $grant_url ?>');
       }
       else  {
        document.getElementById("submit").disabled = true;
  document.getElementById("submit").value = "Please agree to T&C's";
      }

  }; 
</script>
<form>
      <input type="checkbox" name="terms" id="terms" onchange="activateBox(this)" autofocus />
      I Agree to the Terms & Conditions <br>
  
      <input class="button" name="submit" value="Please agree to T&C's" id="submit" title="Continue to the Internet" />
</form>

这应该可行(将 window.open 调用包装在匿名函数中)

<script>
 function disableSubmit() {
  document.getElementById("submit").disabled = true;
 }

  function activateBox(element) {

      if(element.checked) {
        document.getElementById("submit").disabled = false;
        document.getElementById("submit").value = "Press here to go online";
        document.getElementById("submit").onclick= function() { window.open('<?php print $grant_url ?>'); }
        document.getElementById("submit").ontouchstart= function() { window.open('<?php print $grant_url ?>'); }
       }
       else  {
        document.getElementById("submit").disabled = true;
        document.getElementById("submit").value = "Please agree to T&C's";
      }

  }; 
</script>