2010-10-01 61 views
3

我所用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奧雅納只是用於測試和是實際可行的(是的,我是莫蘭)

+1

你的腳本工作正常http://jsbin.com/opipi – 2010-10-01 14:41:57

回答

1

您的代碼工作得很好,一旦不必要的撇號被刪除,演示在jsbin

$("input[name='sid']") 

應該是:

$("input[name=sid]") 


編輯,爲應對雙重檢查我的假設:

我認錯,它工作得很好撇號,太。演示在:jsbin

+0

沒有,我們可以有撇號。 – 2010-10-01 14:43:41

+0

@Avinash:因此,在您評論之前,答案的第二部分。嚴重的是,它正好在那裏:在**編輯**位之後的'


'之下。 – 2010-10-01 14:51:33

+0

在我評論的時候,你有沒有編輯您的文章認真:) – 2010-10-01 14:56:42

1

剛剛獲得的股利引用,然後使用的.text()或.html()來獲取值(即信息存儲在在div)...

1
$("input[name='sid']").live("change", function() { 
    var id = $(this).val(); 
    alert($("#shipping_service_price_" + id).html()) 
    }) 
});