Using jQuery to select checkboxes with click events
Is there a way to use jquery to select all checkboxes on a page that have an associated click event? I considered adding a class, for in开发者_如何学JAVAstance HasClickEvent, that I could use to identify such classes, but I am editing a huge script where click events are sporadically added all over the place and I think this would probably end up being messier, so a single jQuery call would be perfect
jQuery.each($('input[type=checkbox]').data('events'), function(i, event){
jQuery.each(event, function(i, handler){
if(handler.type.toString() == 'click')
{
// do something
}
});
});
To check all
$('.checkbox').attr('checked','checked'); // checkbox is the class for all checboxes to be selected change it with our own
To deselect all
$('.checkbox').removeAttr('checked');
A quick google reveals this plugin, but it's pretty old. You may be able to read the code and see how they are achieving it though :D
If the click
events are attached using the onclick
attribute (instead of added dynamically via JavaScript/jQuery), you can do it like this:
$("input[type=checkbox][onclick]").each(function() {
//All returned elements have an onclick attribute
});
精彩评论