从动态复选框的父级中提取类名

时间:2016-04-01 09:41:32

标签: javascript jquery

我正在尝试从类名中提取数字并将其添加到数组中,但我收到错误消息“对象不支持属性或方法'父级'”。 我刚才正在提取类名。

由于

<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>

3 个答案:

答案 0 :(得分:0)

  

.parent()jQuery包裹元素的方法。 this中的callback代表DOM元素,而不代表jQuery包裹的元素。

使用return $(this).parent().attr('class');

$(".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);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<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>

答案 1 :(得分:0)

$(this)中使用jQuery对象this代替map(),如下所示。

$(this).parent().attr('class');

答案 2 :(得分:0)

你的问题是你没有使用jquery元素来调用parent()方法,而taht来自jQuery。

更改此行,这是$(this)。

runOnUiThread(new Runnable() {
    @Override
    public void run() {
        // change ui
    }
});