Jquery access functions from an $.each loop

故事扮演 提交于 2021-01-29 18:28:12

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!