jQuery实现checkbox全选的方法

前端技术 2023/09/06 JavaScript

本文实例讲述了jQuery实现checkbox全选的方法。分享给大家供大家参考。具体分析如下:

通过checkbox 进行全选和取消全选的操作,如果通过toggle进行处理,则会出现checkbox无法显示对勾的问题。

使用click事件,根据checked属性进行判断即可。

示例:

$(\"#chkRreviewOffline\").click(function(){ 
  if(this.checked){ 
    $(\'#review-offline .btn_checkbox input[type=checkbox]\').each(function(index){ 
      this.checked=true; 
    }); 
  }else{ 
    $(\'#review-offline .btn_checkbox input[type=checkbox]\').each(function(index){ 
      this.checked=false; 
    }); 
  } 
}); 
$(\'#review-offline .btn_checkbox input[type=checkbox]\').each(function(index){ 
  $(this).click(function(){ 
    if(this.checked){ 
      //console.log(\'checked\'); 
    }else{ 
      //console.log(\'not checked\'); 
      $(\"#chkRreviewOffline\").get(0).checked=false; 
    } 
  }); 
});

其中,下面的each()方法用于当页面其它的checkbox有未选中状态,则全选状态取消。

希望本文所述对大家的jQuery程序设计有所帮助。

本文地址:https://www.stayed.cn/item/16299

转载请注明出处。

本站部分内容来源于网络,如侵犯到您的权益,请 联系我

我的博客

人生若只如初见,何事秋风悲画扇。