基于复选框demo(分享)
本篇文章是关于复选框的,有2种形式:1、全选、反选由2个按钮实现;2、全选、反选由一个按钮实现。
复选框demo 全选 反选 苹果 香蕉 梨子 桃子 西瓜
全选 老子 尚书 周易 诗经 孟子 中庸 //全选 functionallSelect(){ $("input[name='fruit']").prop("checked","checked"); $("input[name='cancel']").removeAttr("checked"); } //反选 functionunAllSelect(){ $("input[name='fruit']").removeAttr("checked"); $("input[name='select']").removeAttr("checked"); } //单选 $("#allBook").click(function(){ if(this.checked){ //$("input[name='book']").attr("checked",true); $("input[name='book']").prop("checked","checked"); }else{ //$("input[name='book']").attr("checked",false); $("input[name='book']").removeAttr("checked"); } });
在实践中碰到一个问题——check全选失效。解决办法,使用prop方法代替attr。
$("input[name='book']").attr("checked","checked"); $("input[name='book']").prop("checked","checked");
这或许是和jQuery版本有关。
以上这篇基于复选框demo(分享)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。