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

发布于 2024-11-06 05:21:49 字数 1374 浏览 1 评论 0原文

我有两个协议,用户必须通过选中两个不同的框(每个协议 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>

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.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> </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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

万劫不复 2024-11-13 05:21:49

替换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">

Replace 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">
や三分注定 2024-11-13 05:21:49

也许试试这个:

<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> </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>

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.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> </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>
如果没有你 2024-11-13 05:21:49

为什么您想提醒用户?我认为这不是一个好的经历。
在我看来,您可以禁用提交按钮,直到选中所有复选框。
这是一个例子:
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/

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.net/XZyjw/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文