jQuery多选框选择数量限制方法
2019/6/27 21:26:10
本文主要是介绍jQuery多选框选择数量限制方法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
在网上找到的,可以使用。由于刷新后,多选框会保留选中的,这时数量达到要求后还是可以选择的bug,在前面加个判断数量是否数量达到要求的处理就可以了。
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> <script src="http://code.jquery.com/jquery-1.4.2.min.js"></script> <script> $(function(){ //加上判断是否达到数量要求 if($(":checkbox").size()>=3){ $(":checkbox").removeAttr("checked"); $(":checkbox").attr("disabled","disabled"); $(":checkbox").removeAttr("disabled"); } var num = 0; $(":checkbox").each(function(){ $(this).click(function(){ if($(this)[0].checked) { ++num; if(num == 3) { //alert("最多选择 三项 的上限已满, 其他选项将会变为不可选."); $(":checkbox").each(function(){ if(!$(this)[0].checked) { $(this).attr("disabled", "disabled"); } }); } } else { --num; if(num <= 2) { $(":checkbox").each(function(){ if(!$(this)[0].checked) { $(this).removeAttr("disabled"); } }); } } }); }); }) </script> </head> <body> <input type="checkbox" />上网<br /> <input type="checkbox" />旅游<br /> <input type="checkbox" />逛街<br /> <input type="checkbox" />游戏<br /> <input type="checkbox" />听歌<br /> <input type="checkbox" />购物<br /> </body> </html>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持找一找教程网。
这篇关于jQuery多选框选择数量限制方法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-30React Native常用组件-点击组件
- 2024-05-30uniapp+vue3+uv-ui手机端后台OA管理模板
- 2024-05-29Python网络爬虫的时候json=就是让你少写个json.dumps()
- 2024-05-27React Native常用组件-展示组件
- 2024-05-27React Native常用组件-列表组件
- 2024-05-09vue3开发前端表单缓存自定义指令,移动端h5必备插件
- 2024-05-09React Hooks在class组件中的使用方式
- 2024-03-30[OIDC in Action] 2. 基于OIDC(OpenID Connect)的SSO(纯JS客户端)
- 2024-03-29terraform jsonencode
- 2024-03-13vuex-persist