【发布时间】:2015-03-29 03:25:15
【问题描述】:
这是How to toggle button in button-group on page load in bootstrap3 with jquery的后续问题
我正在尝试读取按钮组中的单选值。我可以在单击按钮时读取值,但不能在更改触发器上读取。
<body>
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-default">
<input type="radio" name="environment" id="staging" value="staging" />Staging
</label>
<label class="btn btn-default">
<input type="radio" name="environment" id="production" value="production" />Production
</label>
</div>
<button class="get-active">Get active</button>
<script>
$(document).ready(function() {
$("#production").parent().button('toggle');
});
$('.get-active').click(function() {
var active = $('.btn-group').find('.active input').val();
alert( active );
});
$("#staging").parent().on('change', function(){
var active = $('.btn-group').find('.active input').val();
alert( active );
});
</script>
</body>
这是 plunker - http://plnkr.co/edit/QwGYXNqNczbgTfMhqey5?p=preview
点击从生产切换到暂存时,它显示一个未定义的警报,点击按钮显示暂存。
【问题讨论】:
标签: javascript jquery twitter-bootstrap-3