Jquery发现不起作用

时间:2015-09-18 11:42:55

标签: javascript jquery

我有一个问题是获取跨度的内容(本例中为Arrière),当其父(li)包含带有“已检查”类的跨度时

这是我的HTML

<fieldset class="attribute_fieldset">
    <label class="attribute_label">Base du siège &nbsp;</label>
        <div class="attribute_list">
            <ul>
                <li>
                    <div class="radio">
                        <span class="checked">
                            <input type="radio" class="attribute_radio" name="group_4" value="26" checked="checked">           
                        </span>
                    </div>
                   <span>Arrière</span>
                </li>
            </ul>
        </div> <!-- end attribute_list -->
</fieldset>

我尝试了这个jquery代码:

$('.attribute_fieldset').each(function( index ) {
    var attribute_label = $(this).children('.attribute_label').html();
    var attributeval = $(this).find('.checked').parent('.radio').next('span').html();
    console.log(attributeval);
    var that = this;    
  $('#product_attributes').append('<li>'+attribute_label.replace('&nbsp;','')+':');

});

我没有为attributeval

定义

2 个答案:

答案 0 :(得分:2)

这应该有效:

$('.attribute_fieldset').each(function() {
   $('#product_attributes').append('<li>' + $(this).find('attribute_label').html().replace(' &nbsp;', '') + ':' + $(this).find('li span.checked').parent().next('span').html() + '</li>');
});

答案 1 :(得分:0)

Instead you can try below code:
$(".attribute_fieldset span").each(function(index, spanElement) {
    if($(spanElement).parent().find("span.checked").length
         && !$(spanElement).hasClass("checked")) {
        console.log(spanElement); // this is the span element you want
        console.log(spanElement.text()); // this is the value "Arrière"
      }
});
相关问题