验证复选框并链接到新页面

时间:2014-02-18 03:42:47

标签: javascript checkbox

我正在尝试制作一个验证复选框的表单。我的想法是你必须选中该框,然后点击提交,然后再转到新页面。所以我是一个复选框,我有一个功能来检查它,但我似乎无法让它加载页面。我在本地保存了两个页面并且它们是正确的,因此它必须是验证功能的问题。任何帮助表示赞赏!

 <!DOCTYPE html>
 <html>
 <head>
 <title>AcceptTerms</title>
 </head>
 <body>
 <h1>Before proceeduing you must agree to our terms and conditions.</h1>
 <br>
 <h3>Do you accept our terms?</h3>
 <form> 
 <p><input type="checkbox" name="checkbox" value="Yes, I accept" />Yes, I accept</p>
 <input type="button" value="Submit" onclick="confirmTerms()">
 </form>    
 <br> 
 <button onclick="openDecline()">Decline Terms</button>
 <script>
 function openDecline()
 {
 window.open("declinepage.html");
 }
 </script>   
 <br>    

 <script type="text/javascript">
 function confirmTerms() {
 if (document.forms[0].checkbox.checked==false { 
 alert("You did not select a box.");
 return false
 }
 if (document.forms[0].checkbox.checked) {
 window.location.href = "acceptpage.html";
 }
 if (document.forms[0].checkbox.checked=false) {
 window.location.href = "declinepage.html";
 }
 </script>
 </body>    
 </html>

2 个答案:

答案 0 :(得分:0)

我对您的脚本进行了一些更改.. 我想这就是你需要的 并为您的复选框设置ID

<!DOCTYPE html>
 <html>
 <head>
 <title>AcceptTerms</title>
 </head>
<body>
     <h1>Before proceeduing you must agree to our terms and conditions.</h1>

    <br>
     <h3>Do you accept our terms?</h3>

    <form>
        <p><input type="checkbox" id="check1" value="Yes, I accept" />Yes, I accept</p>
        <input type="button" value="Submit" onclick="confirmTerms()"/>
    </form>
    <br>
    <button onclick="openDecline()">Decline Terms</button>
        <script>
    function openDecline() {
        window.open("declinepage.html");
    }

    function confirmTerms() {  
        if (!(document.getElementById("check1").checked)) {
            alert("You did not select a box.");
            return false;
        }
        else{
            window.location.href = "acceptpage.html";
        }    
    }
    function openDecline(){
        if(!(document.getElementById("check1").checked)) {
            window.location.href = "declinepage.html";
        }
    }
        </script>
</body>    
 </html>

以下是Fiddle

答案 1 :(得分:0)

它应该是这样的:

<!DOCTYPE html>
<html>
<head>
<title>AcceptTerms</title>
</head>
<body>
 <h1>Before proceeduing you must agree to our terms and conditions.</h1>
 <br>
 <h3>Do you accept our terms?</h3>

 <form> 
  <p><input type="checkbox" name="checkbox" value="Yes, I accept" />Yes, I accept</p>
  <input type="button" value="Submit" onclick="confirmTerms()">
 </form>  

 <br> 
 <button onclick="openDecline()">Decline Terms</button>
 <script>
  function openDecline()
  {
   window.open("www.facebook.com");
  }
 </script>   
 <br>    

 <script type="text/javascript">
 function confirmTerms() {

 if (document.forms[0].checkbox.checked==false) { 
 document.write("Not Checked");
 alert("You did not select a box.");
 return false;
 }

 if(document.forms[0].checkbox.checked) {
 document.write("Checked"); 
 window.location.href = "www.google.com";
 }

 }
 </script>
 </body>    
 </html>