必须勾选最后三个复选框才能提交


Must check last three check boxes to submit

下面是我的复选框列表,这是我的php表单的一部分。我需要写一个脚本,其中只有最后三个复选框必须为表单成功提交检查。我看过其他类似的问题,但似乎找不到我真正想要的东西,我在写剧本时遇到了麻烦。

<input type="checkbox" name="checkbox" value="Send email request">Would you like to receive weekly new via email?<br>
<input type="checkbox" name="checkbox" value="Agree to T&C">I agree to the Terms and Conditions<br> 
<input type="checkbox" name="checkbox" value="No Criminal Records">I have no criminal associations<br>
<input type="checkbox" name="checkbox" value="No Business with any other company">I have no personal or business relationship with any other company<br>

你不需要使用任何JavaScript。

只需在输入元素中添加一个required属性。

<input type="checkbox" required="required" name="checkbox" value="Send email request">Would you like to receive weekly new via email?<br>

请看这里获取更多信息:http://www.the-art-of-web.com/html/html5-checkbox-required/

如果你想要一个自定义消息或响应,那么你需要使用JavaScript来处理它

或者,您可以使用jQuery检查最后3个复选框是否确实在提交前被选中(必需)。考虑这个例子:

<form method="POST" action="index.php" id="form">
    <input type="checkbox" name="checkbox[]" value="Send email request">Would you like to receive weekly new via email?<br>
    <input type="checkbox" name="checkbox[]" value="Agree to T&C">I agree to the Terms and Conditions<br>
    <input type="checkbox" name="checkbox[]" value="No Criminal Records">I have no criminal associations<br>
    <input type="checkbox" name="checkbox[]" value="No Business with any other company">I have no personal or business relationship with any other company<br>
    <input type="submit" name="submit" />
</form>
<script src="jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    $('#form').submit(function(e){
        var count = 0;
        var required_indeces = [1,2,3];
        $('input[type=checkbox]').each(function(index, element){
            if($.inArray(index, required_indeces) !== -1 && $(this).is(':checked')) {
                count++;
            }
        });
        if(count < 3) {
            e.preventDefault();
        }
    });
});
</script>

复选框需要有不同的名称。使用相同的名称意味着您只存储最后选中框的值。尝试以下两个选项之一(对于PHP):

<input type="checkbox" name="checkbox[]" value="Send email request">Would you like to receive weekly new via email?<br>
<input type="checkbox" name="checkbox[]" value="Agree to T&C">I agree to the Terms and Conditions<br> 
<input type="checkbox" name="checkbox[]" value="No Criminal Records">I have no criminal associations<br>
<input type="checkbox" name="checkbox[]" value="No Business with any other company">I have no personal or business relationship with any other company<br>

然后,检查最后三个是否被选中:

if (!isset($_POST['checkbox'][1], $_POST['checkbox'][2], $_POST['checkbox'][3])) {
    echo 'One of the last 3 NOT checked!';
}

或者:

<input type="checkbox" name="checkbox1" value="Send email request">Would you like to receive weekly new via email?<br>
<input type="checkbox" name="checkbox2" value="Agree to T&C">I agree to the Terms and Conditions<br> 
<input type="checkbox" name="checkbox3" value="No Criminal Records">I have no criminal associations<br>
<input type="checkbox" name="checkbox4" value="No Business with any other company">I have no personal or business relationship with any other company<br>

:

if (!isset($_POST['checkbox2'], $_POST['checkbox3'], $_POST['checkbox4'])) {
    echo 'One of the last 3 NOT checked!';
}