使用document.getElementById()。onclick时出现问题

时间:2015-01-19 17:34:49

标签: window.open

我需要创建一个T& C的启动页面,用户勾选该框,然后启用提交按钮并将其转发到正确的页面。

目前我无法弄清楚为什么以下代码使条款复选框成为活动链接 - 单击它以选中该框从window.open加载页面

如果我注释掉两个window.open行,它全部按预期工作但没有链接功能。显然,我正在尝试创建一个适用于所有移动和桌面客户端的页面,否则我会在按钮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>
&#13;
<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>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

这应该有效(将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>
相关问题