我所用JavaScript動態創建一些單選按鈕。我需要弄清楚如何引用一個div(也動態創建的)這裏的文字是樣本HTML:jQuery的直播獲取股利文字
<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>
所以當動態單選按鈕「shipping_service_2」被點擊我想提醒「14.00」 - 「shipping_service_price_2 ID的文本
這裏是我的(非工作)到目前爲止的代碼
$("input[name='sid']").live("change", function() {
var id = $(this).val();
alert($("#shipping_service_price_" + id).text())
})
});
編輯:。 感謝您的答覆該項目的代碼是產生一些不良HTML這就是爲什麼我無法選擇文本。html ab奧雅納只是用於測試和是實際可行的(是的,我是莫蘭)
你的腳本工作正常http://jsbin.com/opipi – 2010-10-01 14:41:57