Javascript:在一个页面上有两个“我同意条款复选框”

时间:2011-05-08 03:17:42

标签: javascript checkbox

我有两个协议,用户必须通过检查两个不同的框(每个协议1个)来“同意”,然后再继续购物车。如果没有选中框,则两个警报都会通知他们“同意”,然后一次继续1,以及每个警告。但是,“提交按钮”不会提交并继续检查两者。我怎样才能使以下工作?

两者都未经检查?警报1

#1未经检查?警报2

#2未经检查?警报3

请帮忙!

<script language="Javascript">
function check_agree (form) {
if (form.agree.checked) {
}
else { alert('You must agree to the application agreement terms before continuing.'); }
}
form.submitButton.disabled = true;
function check_agree_2 (form) {
if (form.agree_2.checked) {
}
else { alert('You must agree to both!'); }
}
form.submit()
</script>

HTML

<form action='http://webisite.com' method='post' onSubmit="return false;">
<p>
 <span style="text-align: center">
 <input type ="checkbox" name="agree" value="anything">

 <b>I Agree To And Understand The Charges That Will Be Processed To My Credit Card</b></span></p>
<p>&nbsp;</p>
<input type ="checkbox" name="agree_2" value="anything">

 <b>I Have Read And Agree To The Member Agreement/Terms And Conditions </b>
 <p>&nbsp;</p>
 <span style="text-align: center">
 <input type="submit" name="submitButton" onClick="check_agree(this.form);check_agree_2(this.form)" value="Continue To Shopping Cart">

</form>

3 个答案:

答案 0 :(得分:2)

替换check_agree,删除check_agree_2,然后更改提交按钮。

function check_agree(form) {
if (form.agree.checked && form.agree_2.checked) {
  return true;
} else if(!form.agree.checked) {
  alert('You must agree to the application agreement terms before continuing.');
} else if(!form.agree_2.checked) {
  alert('You must allow us to charge your credit card.');
}
return false;
}

HTML

<input type="submit" name="submitButton" onClick="check_agree(this.form)" value="Continue To Shopping Cart">

答案 1 :(得分:1)

也许试试这个:

<script type="text/javascript">
    function check_agree (form) {
        if (form.agree.checked && form.agree_2.checked) {
            return true;
        }
        if (form.agree_2.checked) { 
            alert('You must agree to the application agreement terms before continuing.'); 
        }
        else if (form.agree.checked) {
            alert("message #2!");
        }
        else {
            alert('You must agree to both!');
        }
        return false;
    }
</script>

......用这个:

<form action="http://webisite.com" method='post' onsubmit="return check_agree(this);">
<p>
 <span style="text-align: center">
 <input type="checkbox" name="agree" value="anything">

 <b>I Agree To And Understand The Charges That Will Be Processed To My Credit Card</b></span></p>
<p>&nbsp;</p>
<input type="checkbox" name="agree_2" value="anything">

 <b>I Have Read And Agree To The Member Agreement/Terms And Conditions </b>
 <p>&nbsp;</p>
 <span style="text-align: center;">
 <input type="submit" name="submitButton" value="Continue To Shopping Cart">
</form>

答案 2 :(得分:1)

为什么你喜欢alert用户?我不认为这是一次很好的经历 在我看来,您可以禁用提交按钮,直到选中所有复选框 这是一个例子:
HTML:

<input type="checkbox" id="cb1">I Agree To And Understand The Charges That Will Be Processed To My Credit Card<br>
<input type="checkbox" id="cb2">I Have Read And Agree To The Member Agreement/Terms And Conditions
<input type="submit" id="button" value="Submit">

JavaScript:

var cb1 = document.getElementById("cb1"),
    cb2 = document.getElementById("cb2"),
    button = document.getElementById("button");
button.disabled = true;    
cb1.onclick = cb2.onclick = function(){
    if(cb1.checked && cb2.checked){
        button.disabled = false;
    }
    else{
        button.disabled = true;
    }
};

您可以在此处看到现场演示:http://jsfiddle.net/XZyjw/