简体   繁体   English

javascript需要从一系列复选框中选中一个复选框

[英]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.

相关问题 javascript需要从一系列复选框中选中一个复选框 - javascript require a checkbox to be checked from an array of checkboxes 如何从复选框数组中检查至少一个复选框,并且复选框中的文本区域不为空白,且其值为“ other”? - How to check at least one checkbox is checked from the array of checkboxes and the text area is not blank on checkbox with value “other” is checked? 将选中的复选框数组传递给javascript - Pass an array of checked checkboxes to javascript 计算选中的复选框(数组)编号并保存选中的复选框 - Count checked checkbox(array) numbers & saving checked checkboxes 使用Javascript / Jquery遍历表行和复选框,如果选中,则将复选框的属性添加到数组 - Use Javascript/Jquery to iterate through table rows and checkboxes and add attribute of checkbox to array if checked 带有Javascript复选框的动态HTML表格难题-如何从函数外部访问checkbox.check? - Dynamic HTML table with Javascript checkboxes puzzle - how to access checkbox.checked from outside of the function? 如何检查用户是否已使用jQuery / Javascript选中了一组复选框中的至少一个复选框? - How to check whether user has checked at least one checkbox from a group of checkboxes using jQuery/Javascript? 如何检查复选框列表中是否至少选中了一个复选框,其名称属性为数组 - How to check if at least one checkbox is checked from list of checkboxes with it name attribute as array Javascript:通过遍历数组检查复选框是否已选中 - Javascript: check if checkboxes are checked by looping through an array JavaScript推送多维数组仅选中复选框 - Javascript push multidimensional array only checked checkboxes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM