jquery实现radio按钮在分组状态下点击选中,再次点击取消选中
2015-11-13 00:00
846 查看
摘要: 利用data参数对radio的状态做控制,从而实现效果
效果实现共需三个文件:
1、test.html 内容:
2、radioChecked.js 内容
3、jquery1.8.2.js
效果实现共需三个文件:
1、test.html 内容:
<!doctype html> <html> <head> <meta charset="UTF-8"> <title>Document</title> <script type="text/javascript" src="./jquery1.8.2.js"></script> <script type="text/javascript" src="./radioChecked.js"></script> </head> <body> <!-- <input type="radio" name="test" value="a" data-check="1"> <input type="radio" name="test" value="b" data-check="1"> <HR> <input type="radio" name="test1" value="c" data-check="1"> <input type="radio" name="test1" value="d" data-check="1"> <input type="radio" name="test1" value="e" data-check="1"> --> <input type="radio" name="test" value="a"> <input type="radio" name="test" value="b"> <HR> <input type="radio" name="test1" value="c"> <input type="radio" name="test1" value="d"> <input type="radio" name="test1" value="e"> </body> <script> $(function(){ $('.testch').click(function(){ $(this).radioChecked().init(); }); $('.testch').radioChecked().setDataAttr() }); </script> </html>
2、radioChecked.js 内容
;(function($, window, document, undefined){ var MyPlugin = function(obj){ this.$object = obj, this.className = this.getClass(), this.elementName = this.getElementName() //this.options = $.extend({}, this.defaults, opt) } MyPlugin.prototype = { init: function() { if(this.$object.data('check') == '1'){ $(':radio[name="'+ this.elementName +'"]').each(function(){ $(this).data('check','1'); }); this.$object.data('check','2'); alert('选中'); } else if(this.$object.data('check') == '2'){ this.$object.data('check','1'); this.$object.removeAttr('checked'); alert('取消选中'); } }, getClass: function(){ return this.$object.attr('class'); }, getElementName: function(){ return this.$object.attr('name'); }, setDataAttr: function(){ $('.'+this.className).data('check','1'); } } $.fn.radioChecked = function(){ return new MyPlugin(this); } })(jQuery, window, document);
3、jquery1.8.2.js
相关文章推荐
- jquery对象访问
- jquery事件之事件
- 异步上传文件,ajax上传文件,jQuery插件之ajaxFileUpload
- jQuery 自动触发事件
- jQuery tags input 使用
- 浅析JQuery中的html(),text(),val()区别
- jquery 字符串转dom对象及对该对象使用选择器查询
- jQuery tags input 使用
- jquery——九宫格大转盘抽奖
- jquery插件开发
- JQuery的父、子、兄弟节点查找,节点的子节点循环
- jquery,2048小游戏粗制版
- jQuery-1.9.1源码分析系列(五) 回调对象
- jquery的promise实践--连续加载图片
- jquery的promise实践--连续加载图片
- jquery的has、find和filter函数的异同
- jQuery EasyUI Combotree 实现ComboBox的内容过滤
- jquery mobile显示加载提示,并自动消失
- 插件--滚动插件slimScroll
- jquery轮播图