Skip to content

Commit

Permalink
- remove attchange required
Browse files Browse the repository at this point in the history
  • Loading branch information
ekoz committed Mar 27, 2017
1 parent 03dccc1 commit d930cce
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 281 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ Checkbox/Radio 插件

使用方法
-------------------------------------------------
该插件依赖于 [jquery](http://jquery.com/)[attrchange](http://meetselva.github.io/),attrchange插件无需独立引入,只引入jquery.kbase.checkbox.js即可

该插件依赖于 [jquery](http://jquery.com/)
```
<script src="../js/jquery.kbase.checkbox.js" type="text/javascript"></script>
```
Expand All @@ -17,6 +16,8 @@ Checkbox/Radio 插件
$(':checkbox').kbsElem();
$(':radio').kbsElem();
$('#btnRadio').kbsElem(); //不会做初始化
$('input[name="opt"]').kbsElem('checked'); //选中/不选中
```
Demo
--------------------------------------------------
Expand Down
6 changes: 1 addition & 5 deletions example/checkbox.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,7 @@
}
});
$('#btnSelAll').click(function(){
if ($(this).attr('checked')){
$('input[name="opt"]').attr('checked', 'checked');
}else{
$('input[name="opt"]').removeAttr('checked');
}
$('input[name="opt"]').kbsElem('checked');
});
</script>
</body>
Expand Down
124 changes: 0 additions & 124 deletions js/attrchange.js

This file was deleted.

97 changes: 0 additions & 97 deletions js/attrchange_ext.js

This file was deleted.

90 changes: 37 additions & 53 deletions js/jquery.kbase.checkbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,9 @@
},
run: function(){
this.use('jquery.kbase.checkbox.css');
this.use('attrchange.js');
this.use('attrchange_ext.js');
}
}

$.fn.extend({
kbsElem: function(){
//TODO 是否考虑重复初始化的情况
var _thisArr = this;
},
init: function(elem){
var _thisArr = elem;
$(_thisArr).each(function(i, _this){
var _type = $(_this).attr('type');
var _id = $(_this).attr('id')==undefined?'':$(_this).attr('id');
Expand All @@ -69,9 +63,7 @@
//将input元素的属性复制到label
var attrs = $(_this)[0].attributes;
$(attrs).each(function(i, item){
//if (item.name!='type'){
$(_kbsCheckboxItem).attr(item.name, item.value);
//}
$(_kbsCheckboxItem).attr(item.name, item.value);
});
var _text = '';
if ($(_this).attr('text')==null){
Expand All @@ -89,56 +81,19 @@

$(_this).after(_kbsCheckboxItem);
$(_this).attr('id', _id + '_' + Math.random()).addClass('kbs-' + _type + '-input');
$(_this).attrchange({
trackValues: true,
callback: function (event) {
var properties = $(this).attrchange('getProperties');
if (event.attributeName=='checked'){
if (event.oldValue!=event.newValue && event.newValue==undefined){
$(_kbsCheckboxLabel).removeClass('on');
$(_kbsCheckboxItem).removeAttr('checked');
if (builder.isLowerBrowser()){
$(_this).removeAttr('checked');
}else{
$(_this).removeProp('checked');
}
}else{
$(_kbsCheckboxLabel).addClass('on');
$(_kbsCheckboxItem).attr('checked', 'checked');
if (builder.isLowerBrowser()){
$(_this).attr('checked', true);
}else{
$(_this).prop('checked', true);
}
}
}
}
});

if (_type=='checkbox'){
$(_kbsCheckboxItem).on({
kbsCheckedChange: function(){
click: function(){
if ($(_kbsCheckboxLabel).hasClass('on')){
$(_kbsCheckboxLabel).removeClass('on');
$(_kbsCheckboxItem).removeAttr('checked');
if (builder.isLowerBrowser()){
$(_this).removeAttr('checked');
}else{
$(_this).removeProp('checked');
}
$(_this).removeProp('checked');
}else{
$(_kbsCheckboxLabel).addClass('on');
$(_kbsCheckboxItem).attr('checked', 'checked');
//低版本浏览器能识别prop,走进attchange方法,高版本浏览器不会走到attrchange方法中
if (builder.isLowerBrowser()){
$(_this).attr('checked', true);
}else{
$(_this).prop('checked', true);
}
$(_this).prop('checked', true);
}
},
click: function(){
$(this).trigger('kbsCheckedChange');
}
})
}else{
Expand All @@ -151,7 +106,36 @@
});
}
});
return _thisArr;
},
checked: function(elem){
$(elem).each(function(i, item){
$('span[name="' + $(item).attr('name') + '"][value="' + $(item).attr('value') + '"]').click();
});
},
debug: function(o){
if (window.console && window.console.log){
console.log(o);
}
},
fire: function(key, elem, opts){
if (typeof key == 'string') {
if (this[key]){
return this[key](elem, opts);
}else{
this.debug('function [' + key + '] undefined');
}
}
}
}

$.fn.extend({
kbsElem: function(key){
var _this = this;
if (key==undefined || typeof key!='string'){
builder.init(_this);
}else{
builder.fire(key, _this);
}
}
});

Expand Down

0 comments on commit d930cce

Please sign in to comment.