选中/取消选中无复选框

时间:2016-11-11 17:02:13

标签: jquery html

我有一组复选框。一个是条件[]。此数组的最后一个复选框名为" none"。这样,如果用户选中此复选框,则应检查所有其他复选框。

我试图弄清楚如何编写我的if语句,以便在加载页面并检查任何其他复选框时,它需要确保不检查none。反之亦然,如果没有选中其他复选框,那么我需要确保选中了无复选框。

请记住,这些是动态创建的,实际上没有在数据库中。

1 个答案:

答案 0 :(得分:1)

你的问题不是很清楚。

// you need to add a class('.otherCheckboxClass') to all the checkbox other than the none one.

var allOtherChecked = true;
// go trough all checkboxes other than the none one
$( ".otherCheckboxClass" ).each(function() {
  // verify if one of them is not checked
  if(!$(this).is(":checked")){
     // if one is checked, set var to false  
     allOtherChecked = false;
  }

   // if all of them are checked, add check to the none checkbox.
   if(allOtherChecked == true){
     $('.noneCheckBox').prop( "checked", true );
   }

});
相关问题