我正在重新利用 TIMEX 已标记为重复的问题,因为我相信它不是重复的,而且我遇到了同样的问题。
如何在 Bootstrap 中将单选按钮设置为“选中”? https://stackoverflow.com/questions/24463822/how-do-i-set-the-radio-button-to-checked-in-bootstrap
我使用 Bootstrap 按钮作为单选按钮。http://getbootstrap.com/javascript/#buttons http://getbootstrap.com/javascript/#buttons
这是我当前的代码:
<div class="btn-group col-md-2" data-toggle="buttons">
<label class="btn btn-gender btn-default active">
<input type="radio" id="gender" name="gender" value="female"> Girls
</label>
<label class="btn btn-gender btn-default">
<input type="radio" id="gender" name="gender" value="male"> Guys
</label>
</div>
如何使用 JavaScript 将“男性”设置为选中?
这是我对问题的重现:https://jsfiddle.net/JonathanN/ba7d24k3/ https://jsfiddle.net/JonathanN/ba7d24k3/
我无法在 getbootstrap.com 文档中或在 stackoverflow 上轻松找到解决方案,因此我想到了以下解决方案:https://jsfiddle.net/JonathanN/ba7d24k3/2/ https://jsfiddle.net/JonathanN/ba7d24k3/2/
var toggleRadioButton = function ($parent, name, value) {
var $allButtons = $parent.find('input[name="' + name + '"]');
$allButtons.prop('checked', false);
$allButtons.parents('label.btn').removeClass('active');
var $targetButton = $parent.find('input[name="' + name + '"][value="' + value + '"]');
$targetButton.prop('checked', true);
$targetButton.parents('label.btn').addClass('active');
};
我使用了 jQuery,但它也可以用更详细的 JavaScript 进行编码。希望我已经确定这不是基本的如何使用 javascript 设置单选按钮问题的重复。
我的问题:有更简单的方法吗?