[英]How to find all elements that belong to a dynamically added class by jquery?
I have dynamically (by jquery) made HTML elements that have classes .foo
and .bar
, whose all elements are inputs
of type checkboxes
. 我已经动态地(通过jquery)使HTML元素具有类
.foo
和.bar
,它们的所有元素都是类型checkboxes
inputs
。 I have a button checkAll
that checks all the boxes and pushes its id
s into an array called selectedBuckets
.This button wasn't made dynamically. 我有一个
checkAll
按钮,它检查所有框并将其id
推送到一个名为selectedBuckets
的数组中。该按钮不是动态创建的。 It looks something like this: 看起来像这样:
var bucketClasses = [$('.foo'), $('.bar')];
$('#checkAll').click(function () {
var isAdd = true;
var selectedBuckets = [];
bucketClasses.forEach(function (bucketClass) {
bucketClass.each(function () {
this.checked = isAdd;
if (isAdd)
selectedBuckets.push(this.id);
})
});
});
The problem is that the function is not able to find out the HTML elements that have a class .foo
(or .bar
for that matter). 问题在于该函数无法找出具有
.foo
类(或.bar
类)的HTML元素。 By putting breakpoints, I see that bucketClass
is an empty array while ideally it should be an array of all the checkboxes
that have class as .foo
. 通过放置断点,我看到
bucketClass
是一个空数组,而理想情况下,它应该是所有类为.foo
的checkboxes
的数组。 What am I doing wrong? 我究竟做错了什么?
You should place your var in the click's callback function: 您应将var放入点击的回调函数中:
$('#checkAll').click(function () {
var bucketClasses = [$('.foo'), $('.bar')];
// all other code as is
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.