2014-08-04 5 views
0
<div style="display: block;" class="Topping-details" id="4"> 
    <section id="topping_tsection_4"> 
     <aside> 
     <h6 class="tdHeading">Quantity  1</h6> 

      <section class="secclass"><a topping_id="1" topp_name="Honey with Chocolate Sauce 10 ML" top_price="25" class="tpActive" >Honey with Chocolate Sauce 10 ML</a> 
     </section> 

     <section class="secclass"><a topping_id="2" topp_name="Honey with Carmel 10 ML" top_price="25" class="tpActive" >Honey with Carmel 10 ML</a> 
     </section> 
     </aside> 


     <aside> 

     <h6 class="tdHeading">Quantity 2</h6> 
     <section class="secclass"><a qt_val="111" top_price="25" >Honey with Chocolate Sauce 10 ML</a> 
     </section> 

     <section class="secclass"><a qt_val="111" top_price="25" class="tpActive">Honey with Carmel 10 ML</a> 
     </section> 

     </aside> 
    </section> 
</div> 

Привет,Как проверить hasclass при использовании для цикла

Это мой HTML ответ, на нажатие кнопки я пытаюсь получить значение атрибута top_price последнего в сторону секции

Я попробовал это так, но его не workng

http://jsfiddle.net/6y6h7/1/

 $(document).on("click", ".work", function() { 

var id_attr_val=4; 

$(this).find(".Topping-details").find('section#topping_tsection_'+id_attr_val+' aside:last').find('.tpActive').each(function() { 
    var top_price = $(this).attr("top_price"); 
    alert(top_price); 
     }); 

}); 

ответ

2

потому что .Topping-details не являются потомками При нажатии кнопки (.work), вам нужно сделать документ широкого поиска, чтобы найти те элементы, вместо того, чтобы пытаться найти те элементы в щелкнутом элементе

$(".Topping-details").find('section#topping_tsection_' + id_attr_val + ' aside:last').find('.tpActive').each(function() { 
    var top_price = $(this).attr("top_price"); 
    console.log(top_price); 
}); 

Демы: Fiddle

Но так как Id элемента должен быть уникальным, вы можете использовать просто для поиска целевого элемента с ид-селектор

$('#topping_tsection_' + id_attr_val + ' aside:last').find('.tpActive').each(function() { 
    var top_price = $(this).attr("top_price"); 
    console.log(top_price); 
}); 

Демо: Fiddle

Смежные вопросы