我的表单上有一个引导按钮组,作为单选按钮工作。如果用户选择其中一个选项(在这种情况下"单日和#34;),我需要对表单的其余部分进行一些验证。如果验证失败,我需要"取消选择"单日 - 基本上撤消单选按钮选择,以便"多天"仍然是视觉选择。
我创造了一个简单的小提琴示例w /我尝试过的三个解决方案:http://jsfiddle.net/u5Ab4/
解决方案1 - 由于它是一个按钮组,我想我可以切换组以更改选择
解决方案2 - 由于群组切换不起作用,我可能需要切换各个按钮吗?
解决方案3 - 切换是一个破灭,手动添加/删除活动类怎么样?
JSFiddle Code
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");
});
答案 0 :(得分:0)
我在同事的帮助下弄明白了。我的例子很简单,所以我最终做的是切换按钮组(解决方案1)并添加一个返回false;相关的if语句。