2010-03-23 58 views
5

海爲選中的單選按鈕的文本標籤,顯示在Jquery的

我有一組單選按鈕,我可以能夠挑選使用jQuery選定的值而不是選擇的值的文本標籤。

爲前:

<input type="radio" value="1" name="priority">High</input> 
<input type="radio" value="2" name="priority">Medium</input> 
<input type="radio" value="3" name="priority">Low</input> 

jQuery代碼挑選擇的值

jQuery('input:radio[name=priority]').change(function() 
{ 
var priority_type=jQuery(this).attr("value"); 
     alert(priority_type); 
} 

輸出將是以下(1,2,3)

現在我要求任何一個是,我想顯示所選值的標籤 例如(高或低或中等)取決於單選按鈕的選擇。

希望這會有所幫助。讓我知道你是否有任何問題。請幫助我完成此任務

回答

6

我的第一個想法是text()將正常工作。不幸的是,沒有收音機的innerText。您可以使用標籤和收音機,並將for屬性指定爲單選按鈕ID。像

.text()

jQuery('input:radio[name=priority]').change(function() 
{ 
    var id = $(this).attr("id"); 
    alert($('label[for='+id+']').text()); 
} 

<input type="radio" name="priority" value="1" id="rdLow" /> 
<label for="rdLow">Low</label> 
<input type="radio" name="priority" value="2" id="rdMedium" /> 
<label for="rdMedium">Medium</label> 
<input type="radio" name="priority" value="3" id="rdHigh" /> 
<label for="rdHigh">High</label> 

東西見一個working demo

+0

拉胡爾,我 – Thinker 2010-03-23 12:06:27

+0

這個代碼不工作更新我的答案。 – rahul 2010-03-23 12:25:47