如何在提交前检查复选框数组的内容

How to check the contents of checkbox array before submit

本文关键字:数组 复选框 检查 提交      更新时间:2023-09-26

我有一个这样的php函数

function printSelectedMembers($id) {
    $full_name = $this->getUserName($id);
    echo '<dl>';
    echo '<dt><label for="event_show">Selected member :</label></dt>';
    echo '<input type="checkbox" name="pvtContacts[]"  checked="checked" value="'.$id.'"/>'.$full_name;
    echo '</dl>';
}

在这里,如果成员已经在复选框数组(pvtContacts[])中,我想删除条目。在提交表单之前如何检查数组的内容?

在注释中,你不能用php检查字段,因为它们是在表单提交后检查的,但是你可以使用javascript来检查复选框是否被选中。

<script type="text/javascript">
function checkForm(form) { 
    for (i=0; i<form.elements['pvtContacts[]'].length; i++) { 
        // this is where you start the checks or altering the form
        // remove (but you have to setup an unique id for each field):
        // var thisId = document.getElementById(form.elements['pvtContacts[]'][i].id);
        // thisId.parentNode.removeChild(thisId);
        if (form.elements['pvtContacts[]'][i].checked != true) { 
            // no all checkboxes are checked
            return false; 
        } 
    } 
    return true; 
}
</script>