问题
I have a list of checkboxes and want to do something with all the names of the checkboxes. But I can't seem to access the HTML objects:
$('.update').click(function(){
$('input[type=checkbox]:checked').each(function(i,elem){
console.log(elem);
elem.hide();
});
});
This produces TypeError: elem.hide is not a function
But the console.log(elem) shows: <input type="checkbox" name="TV">
How can I access each element?
回答1:
You are accessing the DOM node directly by using elem
. You need to pass it to $()
to get a jQuery object with access to .hide()
and other jQuery methods:
$('.update').click(function() {
$('input[type=checkbox]:checked').each(function(i, elem) {
$(elem).hide();
});
});
回答2:
elem is DOM element, not jquery type. You can use $(elem).hide()
instead
来源:https://stackoverflow.com/questions/28733627/jquery-access-functions-from-an-each-loop