Javascript 从动态复选框的父级提取类名
我试图从类名中提取数字并将其添加到数组中,但收到错误消息“Object不支持属性或方法'parent'”。 我现在正在提取类名 谢谢Javascript 从动态复选框的父级提取类名,javascript,jquery,Javascript,Jquery,我试图从类名中提取数字并将其添加到数组中,但收到错误消息“Object不支持属性或方法'parent'”。 我现在正在提取类名 谢谢 <ul id="catbox"> <li class="cat-item cat-item-2"> <a href="http://oursite/events/category/internal-events/">Internal Events</a> <ul clas
<ul id="catbox">
<li class="cat-item cat-item-2">
<a href="http://oursite/events/category/internal-events/">Internal Events</a>
<ul class='children'>
<li class="cat-item cat-item-8">
<a href="http://oursite/events/category/internal-events/civic-and-ceremonial/">Civic and Ceremonial</a>
</li>
<li class="cat-item cat-item-6">
<a href="http://oursite/events/category/internal-events/economy-and-skills-education-tourism-town-centre-business-events/">Economy and Skills – Education, Tourism, Town Centre, Business Events</a>
</li>
<li class="cat-item cat-item-7">
<a href="http://oursite/events/category/internal-events/health-and-social-care/">Health and Social Care</a>
</li>
<li class="cat-item cat-item-13">
<a href="http://oursite/events/category/internal-events/other-internal-events/">Other</a>
</li>
<li class="cat-item cat-item-5">
<a href="http://oursite/events/category/internal-events/safer-communities-vibrant-communities-leisure-trust/">Safer Communities – Vibrant Communities, Leisure Trust</a>
</li>
</ul>
</li>
</ul>
<script type="text/javascript">
$("#test").load("/events/cal-page/");
$(".cat-item").prepend("<input type=\"checkbox\" > ");
$(":checkbox").change(function () {
if (this.checked) {
var values = $('input:checkbox:checked').map(function () {
return this.parent().attr('class');
}).get();
alert(values);
$("#test").load("/events/cal-page/?q=" + number + "");
}
});
</script>
-
-
-
-
-
-
$(“#测试”).load(“/events/cal page/”);
$(“.cat项”)。前缀(“”);
$(“:复选框”).change(函数(){
如果(选中此项){
var值=$('input:checkbox:checked').map(函数(){
返回此.parent().attr('class');
}).get();
警报(数值);
$(“#测试”).load(“/events/cal page/?q=“+number+”);
}
});
.parent()
是jQuery
包装元素的方法这个回调中的将表示DOM
元素,而不是jQuery
包装的元素
使用return$(this.parent().attr('class')代码>
$(“.cat项”)。前缀(“”);
$(“:复选框”).change(函数(){
如果(选中此项){
var值=$('input:checkbox:checked').map(函数(){
返回$(this.parent().attr('class');
}).get();
警报(数值);
}
});代码>
-
-
-
-
-
-
在map()
中使用jQuery对象$(this)
而不是this
,如下所示
$(this).parent().attr('class');
您的问题是您没有使用jquery元素调用parent()方法taht is from jquery
将此行更改为$(this)
return $(this).parent().attr('class');