简体   繁体   中英

Get text from span with a specific class

I have the below HTML and JQuery code and i am needing some help to do the following, i have some radio buttons with freight types, when the user choose one of them i need to get its price, it is in the span with the class price . I've tried to use .closest() but i am getting and empty result (JQuery v1.7.2). Can anybody help me to get this?

HTML

<div class="freight">
    <input type="radio" name="freight-type" id="FX" value="FX" />
    <label for="FX">
        <span class="blue">Fedex</span>- 
        <strong>US$ <span class="price">12.42</span> </strong>
        <span class="small-text"> (Delivery time: <strong>3 business days)</strong></span>
    </label>
</div>

JQuery

$("input[name=freight-type]").change(function() {

    alert( $(this).closest(".price").text() )

});

http://jsfiddle.net/4AEyp/

an example

$("input[name='freight-type']").change(function() {
   alert($(this).parent().find('.price').text());
});​

Try this:

$("input[name=freight-type]").change(function() { 
    alert( $(this).parent().find(".price:first").text() )
});

closest() goes UP through the DOM tree, so it would starting looking in div.fregith and then go up the DOM tree from there.

try this as your selector:

$(this).next('label').find('.price')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM