0

我想对多个复选框应用验证,但问题是如何做到这一点?

<?php
mysql_connect("localhost","root","thisis");
mysql_select_db("my_database");

if(isset($_GET["q"]))
{
$my_q = $_GET['q'];
$q="select * from subjects where subj_code='$my_q'";
$rs=mysql_query($q);

    for($i=0;$i<mysql_num_rows($rs);$i++)
    {
        $rd=mysql_fetch_object($rs);

?>

        <input type="checkbox" name="subj[]" value="<?php echo $rd->subj_name; ?>" /><?php echo $rd->subj_name; ?>
        <br />
<?php
    }

}
?>
4

2 回答 2

0

试试下面的代码。

obj = document.form1.elements("subj[]");
for (i = 0; i < obj.length; i++) {
    if (obj[i].checked) {
        alert("Your text");
        return true;
    }
}
于 2013-02-25T12:42:23.923 回答
0

尝试这个:

if($('input[name="sub[]"]:checked').length == 0) {
   alert('No checkbox is checked');
   return false;
}
于 2015-10-08T08:35:31.223 回答