0

我试图弄清楚如何使用javascript为一组复选框(php数组)创建一个必填字段。

    <?php 
    $hobbiesIndex=0;
    foreach ($hobbies_checkbox as $option => $options){?>
   <input type='checkbox' id='hobbies' name='hobbies[<?php echo $hobbiesIndex; ?>]' value='<?php echo $option;?>'
   <?php if(!empty($hobbies[$hobbiesIndex])){ echo "checked='checked'";} ?> /><label for='hobbies<?php echo $hobbiesIndex ; ?>'><?php echo $options;?></label><br />

  <?php $hobbiesIndex++; }?> 

html 代码浏览器看到的内容;p

      <input type='checkbox' id='hobbies0' name='hobbies[0]' value='1'/><label for='hobbies0'>football</label><br />

      <input type='checkbox' id='hobbies1' name='hobbies[1]' value='2'/><label for='hobbies1'>soccer</label><br />

      <input type='checkbox' id='hobbies2' name='hobbies[2]' value='3'/><label for='hobbies2'>baseball</label><br />

     <input type='checkbox' id='hobbies3' name='hobbies[3]' value='4'/><label for='hobbies3'>tennis</label><br /> 
4

1 回答 1

0
if($('input:checkbox[name="hobbies[]"]:checked').length === 0){
$("#multichk").after('<span class="error">Please choose at least one.</span>');
hasError = true;
}
于 2012-05-25T05:39:53.990 回答