admin管理员组文章数量:1384604
I have two agreements that a user must "agree" to by checking two different boxes (1 for each agreement) before continuing on to the shopping cart. If no boxes are checked, both alerts notify them to "agree" before continuing on 1 at a time, as well as each individually. However, the "submit button" wont submit and continue on when both are checked. How can I get the following to work?
Both unchecked? Alert 1
#1 unchecked? Alert 2
#2 unchecked? Alert 3
Please help!
<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='' 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> </p>
<input type ="checkbox" name="agree_2" value="anything">
<b>I Have Read And Agree To The Member Agreement/Terms And Conditions </b>
<p> </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>
I have two agreements that a user must "agree" to by checking two different boxes (1 for each agreement) before continuing on to the shopping cart. If no boxes are checked, both alerts notify them to "agree" before continuing on 1 at a time, as well as each individually. However, the "submit button" wont submit and continue on when both are checked. How can I get the following to work?
Both unchecked? Alert 1
#1 unchecked? Alert 2
#2 unchecked? Alert 3
Please help!
<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.' 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> </p>
<input type ="checkbox" name="agree_2" value="anything">
<b>I Have Read And Agree To The Member Agreement/Terms And Conditions </b>
<p> </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>
Share
Improve this question
edited May 8, 2011 at 3:24
CRM
4,6454 gold badges28 silver badges34 bronze badges
asked May 8, 2011 at 3:17
PatPat
511 gold badge2 silver badges6 bronze badges
3 Answers
Reset to default 2Replace check_agree, remove check_agree_2, and change the submit button.
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">
Perhaps try this:
<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>
...with this:
<form action="http://webisite." 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> </p>
<input type="checkbox" name="agree_2" value="anything">
<b>I Have Read And Agree To The Member Agreement/Terms And Conditions </b>
<p> </p>
<span style="text-align: center;">
<input type="submit" name="submitButton" value="Continue To Shopping Cart">
</form>
Why do you like to alert
the user? I don't think it's a good experience.
In my opinion, you can just disable the submit button until all the checkboxes are checked.
Here is an example:
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;
}
};
You can see a live demo here: http://jsfiddle/XZyjw/
本文标签: checkboxJavascript two quotI agree to terms checkboxesquot on one pageStack Overflow
版权声明:本文标题:checkbox - Javascript: two "I agree to terms checkboxes" on one page - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744527693a2610842.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论