2013-05-21 31 views
6

我有一個只讀表單,我想填充從ajax查詢返回的值。我不明白的是獲取特定單選按鈕的ID的正確語法。我知道數據庫中存在什麼值,但是如何獲取單選按鈕的ID以更改單選按鈕屬性?Jquery通過測試值得到單選按鈕的ID

例如,在「名稱」的單選按鈕選項== Criteria1Score」,怎麼我返回輸入其中value ==好

這裏是形式結構的例子的id:

<legend>Criteria1</legend> 
<label class="radio inline"> 
    Great 
    <input id="Criteria1Score1" class="Score" name="Criteria1Score" type="radio" value="Great"> 
</label> 
<label class="radio inline"> 
    Good 
    <input id="Criteria1Score2" class="Score" name="Criteria1Score" type="radio" value="Good"> 
</label> 
<label class="radio inline"> 
    Bad 
    <input id="Criteria1Score3" class="Score" name="Criteria1Score" type="radio" value="Bad"> 
</label> 

回答

23

$('input[type=radio][name=Criteria1Score]:checked').attr('id')

fiddle例如

+0

感謝所有這些答案的工作。我錯過了多屬性選擇器。現在我得到了它的工作。 – manisha

+0

對於任何使用現代jQuery版本瀏覽正確版本的人,都是:$('input [type = radio] [name = Criteria1Score]:checked')。prop('id') –

0

可以遍歷這些輸入到你所需要的:

$("input[name='Criteria1Score']").each(function() { 
    if (this.value == "Good") { 
     //do something with this input 
    } 
}); 
0
$("input[value='Good']").attr("id"); 

我F你想它的價值

0

選擇輸入您可以嘗試

var selected_Id = $('input[name="Criteria1Score"]:selected').attr('id'); 

編輯:對不起沒有看到第二部分。如果您想檢查==良好的價值,你可以做

if ($('input[name="Criteria1Score"]:selected').val() == 'Good') { 
    //do something 
} 
0

我通過檢查得到了它。

var selected_Id = $('input[name="Criteria1Score"]:checked').attr('id'); 

if ($('input[name="Criteria1Score"]:checked').val() == 'Good') { 
    //do something 
}