2014-12-07 72 views
1

我使用的引導按鈕,看起來像普通的按鈕,但他們的行爲像收音機。jQuery - 從按鈕組中獲取活動按鈕(輸入類型=「收音機」)

<div class="btn-group-horizontal" data-toggle="buttons" id ="Group"> 
    <label class="btn btn-sm btn-info btnSelect active"> 
    <input type="radio" name="options" id="Option 1" autocomplete="off" checked> Option 1 
    </label> 
    <label class="btn btn-sm btn-info btnSelect"> 
    <input type="radio" name="options" id="Option 2" autocomplete="off"> Option 2 
    </label> 
</div> 

如何獲取活動按鈕的ID /文本?

謝謝!

+1

的可能重複[?我怎樣才能獲得通過jQuery選擇哪個無線電(http://stackoverflow.com/questions/596351/how-can-i-get -which-radio-is-selected-via-jquery) – 2014-12-07 09:44:40

回答

5

溶液如下:

我不得不addiong ID來改變HTML到標籤

<div class="btn-group-horizontal" data-toggle="buttons" id ="Group"> 
    <label class="btn btn-sm btn-info btnSelect active" **id="Option 1"**> 
    <input type="radio" name="options" id="Option 1" autocomplete="off" checked> Option 1 
    </label> 
    <label class="btn btn-sm btn-info btnSelect" **id="Option 2"**> 
    <input type="radio" name="options" id="Option 2" autocomplete="off"> Option 2 
    </label> 
</div> 

,並通過使用以下的jQuery:

var answer= ''; 
      $('#Group .active').each(function(){ 
       answer= $(this).attr('id'); 
      }); 
0

可以嘗試使用each() & is()。例如:

var activeId = ''; 
$('input[type=radio]').each(function(){ 
    if($(this).is(':checked')){ 
     activeId = $(this).attr('id'); 
     //alert(activeId); 
     return false; 
    } 
}); 
+0

我怎樣才能限制這個特定的按鈕組(在這種情況下id =「Group」? – Vika 2014-12-07 09:50:45

+0

你可以使用'$('input [name =「options」]')。each(...)'; – Riad 2014-12-07 09:55:39

+0

@Vika然後可以使用$('#Group input [type = radio]')。each()'代替 – MH2K9 2014-12-07 10:00:51