Created
June 14, 2012 08:18
-
-
Save kazukeyan/2928968 to your computer and use it in GitHub Desktop.
特定の要素&イベントでチェックボックスを一括でON/OFFできるようにするJS(jQuery依存)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function AllCheckToggle(){ | |
var target = $("form[name=f] input[type=checkbox]"); | |
var trigger = $("#allCheckToggle"); | |
var event = "change"; | |
this.execute = function() { | |
//checkboxのチェックがトリガーじゃない場合の想定が抜けてる | |
if (trigger.is(':checked')) { | |
this.check(); | |
} else { | |
this.uncheck(); | |
} | |
} | |
this.check = function(){ | |
target.each(function(i){ | |
$(target[i]).attr("checked", true); | |
}); | |
}, | |
this.uncheck = function(){ | |
target.each(function(i){ | |
$(target[i]).attr("checked", false); | |
}); | |
} | |
this.addEvent = function(){ | |
var toggle = (function(element){ | |
return function() {element.execute()}; | |
})(this); | |
//トリガー対象が複数ある場合が抜けてる | |
trigger.bind(event, toggle); | |
} | |
} | |
//利用方法 | |
//引数でtarget, trigger, eventを指定できるようにすると柔軟性が増す? | |
var allCheckToggle = new AllCheckToggle(); | |
allCheckToggle.addEvent(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment