2014-12-07 4 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 - 라디오 - 선택된 - 통해 - jquery) –

답변

5

이 솔루션은 다음과 같은되었습니다

내가 ID를 addiong하여 HTML을 변경했다 lable에

<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 = "그룹"제한 할 수 있습니까? – Vika

+0

당신은'$ ('input [name = "options"]')을 사용할 수 있습니다. 각각 (...)'; – Riad

+0

@Vika 그러면 대신 $ ('# 그룹 입력 [type = 라디오]'). each()'를 사용할 수 있습니다. – MH2K9

관련 문제