[英]jQuery - get active button from buttons group (input type = “radio”)
我正在使用引导程序按钮,它们看起来像常规按钮,但它们的作用类似于收音机。
<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 /文本?
谢谢!
解决方案如下:
我必须通过标签添加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');
});
您可以只使用一个简单的CSS选择器
$(document).ready(function () {
alert($('.active input').prop('id'));
});
可以尝试使用each()
和is()
。 例:
var activeId = '';
$('input[type=radio]').each(function(){
if($(this).is(':checked')){
activeId = $(this).attr('id');
//alert(activeId);
return false;
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.