I have some radio buttons that are created dynamically with javascript. I need to figure out how to reference the text in a div (also created dynamically) Here is sample html:
<div>
<div class="div_ceck"><input id="shipping_service_1" name="sid" value="1" type="radio"></div>
<div class="free"><label for="shipping_service_1" id="shipping_service_price_1">Free</label></div>
<br class="clr">
<div class="div_ceck"><input id="shipping_service_2" name="sid" value="2" type="radio"></div>
<div class="free"><label for="shipping_service_2" id="shipping_service_price_2">14.00</label></div>
</div>
So when the dynamic radio button "shipping_service_2" is clicked I would like to alert "14.00" - "shipping_service_price_2 id's text.
Here is my (non-working) code so far
$("input[n开发者_JS百科ame='sid']").live("change", function() {
var id = $(this).val();
alert($("#shipping_service_price_" + id).text())
})
});
EDIT: Thanks for the responses. The projects code was producing some malformed html which is why I was unable to select the text. the html above was just used for testing and does in fact work (yes, I am a moran)
Your code works perfectly well, once the unnecessary apostrophes are removed, demo at jsbin:
$("input[name='sid']")
should be:
$("input[name=sid]")
Edited, in response to double-checking my assumption:
I stand corrected, it works perfectly well with the apostrophes, too. Demo at: jsbin.
just get the div reference and then use .text() or .html() to get the value (i.e., info stored in the div)...
$("input[name='sid']").live("change", function() {
var id = $(this).val();
alert($("#shipping_service_price_" + id).html())
})
});
精彩评论