[英]javascript require a checkbox to be checked from an array of checkboxes
I'm trying to figure out how to make a required field for a set of checkboxes (php array) using javascript. 我试图弄清楚如何使用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 code what the browser sees ;p 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 />
if($('input:checkbox[name="hobbies[]"]:checked').length === 0){
$("#multichk").after('<span class="error">Please choose at least one.</span>');
hasError = true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.