我的表单上有一个引导按钮组,用作单选按钮。如果用户选择其中一个选项(在本例中为“单日”),我需要对表单的其余部分进行一些验证。如果验证失败,我需要“取消单击”单日 - 基本上撤消单选按钮选择,以便仍然在视觉上选择“多日”。
我创建了一个简单的小提琴示例,其中包含我尝试过的三种解决方案:http: //jsfiddle.net/u5Ab4/
解决方案 1 - 由于它是一个按钮组,我想我可以切换组以更改选择
解决方案 2 - 由于组切换不起作用,也许我需要切换各个按钮?
解决方案 3 - 切换是失败的,手动添加/删除活动类怎么样?
JSF中间代码
HTML:
<div id="session_type" class="btn-group" data-toggle="buttons-radio">
<button type="button" class="btn">Single Day</button>
<button type="button" class="btn active">Multi Day</button>
</div>
JS:
$('#session_type > button.btn').on("click", function () {
//attempt 1 - since it's a radio button-style button group, i thought i could toggle the whole group (ie switch which button was selected)
$('#session_type').button("toggle");
//attempt 2 - toggle the button that was clicked (ie. single day) to unselect it (might need to toggle the other button as well, to select it instead, but this doesnt work at all)
//$(this).button("toggle");
//manually add/remove the active class from each button (again, would probably need to add the class to the other button as well, but it doesnt work)
//$(this).removeClass("active");
});