我有一个 Twitter Bootstrap 按钮收音机并将 onclick 事件挂钩到它。但是如何检查哪些按钮被触发了?
我的第一个想法是简单地检查“活动”类,但这应该会产生竞争条件(结果取决于是先处理 Twitter Bootstrap 事件还是我自己的 onclick 事件)。
我有一个 Twitter Bootstrap 按钮收音机并将 onclick 事件挂钩到它。但是如何检查哪些按钮被触发了?
我的第一个想法是简单地检查“活动”类,但这应该会产生竞争条件(结果取决于是先处理 Twitter Bootstrap 事件还是我自己的 onclick 事件)。
我看到了很多复杂的答案,而这在 Bootstrap 3 中非常简单:
第 1 步:使用官方示例代码创建您的单选按钮组,并给容器一个 id:
<div id="myButtons" class="btn-group" data-toggle="buttons">
<label class="btn btn-primary active">
<input type="radio" name="options" id="option1" autocomplete="off" checked> Radio 1 (preselected)
</label>
<label class="btn btn-primary">
<input type="radio" name="options" id="option2" autocomplete="off"> Radio 2
</label>
<label class="btn btn-primary">
<input type="radio" name="options" id="option3" autocomplete="off"> Radio 3
</label>
</div>
第 2 步:使用这个 jQuery 处理程序:
$("#myButtons :input").change(function() {
console.log(this); // points to the clicked input button
});
这真的很烦人。我最终使用的是:
首先,创建一组没有data-toggle
属性的简单按钮。
<div id="selector" class="btn-group">
<button type="button" class="btn active">Day</button>
<button type="button" class="btn">Week</button>
<button type="button" class="btn">Month</button>
<button type="button" class="btn">Year</button>
</div>
接下来,编写一个事件处理程序,通过“激活”单击的按钮和“停用”所有其他按钮来模拟单选按钮效果。(编辑:从评论中集成Nick的更清洁版本。)
$('#selector button').click(function() {
$(this).addClass('active').siblings().removeClass('active');
// TODO: insert whatever you want to do with $(this) here
});
我会使用更改事件而不是这样的点击:
$('input[name="name-of-radio-group"]').change( function() {
alert($(this).val())
})
对于 Bootstrap 3,默认的单选/按钮组结构是:
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-primary">
<input type="radio" name="options" id="option1"> Option 1
</label>
<label class="btn btn-primary">
<input type="radio" name="options" id="option2"> Option 2
</label>
<label class="btn btn-primary">
<input type="radio" name="options" id="option3"> Option 3
</label>
</div>
您可以像这样选择活动的:
$('.btn-primary').on('click', function(){
alert($(this).find('input').attr('id'));
});
不要使用 data-toggle 属性,以便您可以自己控制切换行为。所以它将避免“竞争条件”
我的代码:
按钮组模板(用 .erb 编写,嵌入 ruby 用于 ruby on rails):
<div class="btn-group" id="featuresFilter">
<% _.each(features, function(feature) { %> <button class="btn btn-primary" data="<%= feature %>"><%= feature %></button> <% }); %>
</div>
和 javascript:
onChangeFeatures = function(e){
var el=e.target;
$(el).button('toggle');
var features=el.parentElement;
var activeFeatures=$(features).find(".active");
console.log(activeFeatures);
}
单击按钮后将触发 onChangeFeatures 函数。
I needed to do the same thing for a chart where you could select the period of the data that should be displayed.
Therefore I introduced the CSS class 'btn-group-radio' and used the following unobtrusive javascript one-liner:
// application.js
$(document).ready(function() {
$('.btn-group-radio .btn').click(function() {
$(this).addClass('active').siblings('.btn').removeClass('active');
});
});
And here is the HTML:
<!-- some arbitrary view -->
<div class="btn-group btn-group-radio">
<%= link_to '1W', charts_path('1W'), class: 'btn btn-default active', remote: true %>
<%= link_to '1M', charts_path('1M'), class: 'btn btn-default', remote: true %>
<%= link_to '3M', charts_path('3M'), class: 'btn btn-default', remote: true %>
<%= link_to '6M', charts_path('6M'), class: 'btn btn-default', remote: true %>
<%= link_to '1Y', charts_path('1Y'), class: 'btn btn-default', remote: true %>
<%= link_to 'All', charts_path('all'), class: 'btn btn-default', remote: true %>
</div>
Looking at the example HTML for radio buttons on the Twitter Bootstrap page (http://twitter.github.com/bootstrap/base-css.html#forms), you can see that each input has a unique ID attribute, i.e. optionsRadios1
and optionsRadios2
.
The relevant HTML example snippet is included here for completeness:
<div class="controls"> <label class="radio"> <input type="radio" checked="" value="option1" id="optionsRadios1" name="optionsRadios"> Option one is this and that—be sure to include why it's great </label> <label class="radio"> <input type="radio" value="option2" id="optionsRadios2" name="optionsRadios"> Option two can is something else and selecting it will deselect option one </label> </div>
So you can use a jQuery click event, and then use the this
reference to look at the id of the HTML element that was clicked.
$('.controls').find('input').bind('click',function(event){ if($(this).attr('id')==='optionsRadios1'){ alert($(this).attr('id')); } else { //... call some other function } });
如果你的 html 与示例类似,那么点击事件是在标签上产生的,而不是在输入中,所以我使用下面的代码: Html 示例:
<div id="myButtons" class="btn-group" data-toggle="buttons">
<label class="btn btn-primary active">
<input type="radio" name="options" id="option1" autocomplete="off" checked> Radio 1 (preselected)
</label>
<label class="btn btn-primary">
<input type="radio" name="options" id="option2" autocomplete="off"> Radio 2
</label>
</div>
事件的 Javascript 代码:
$('#option1').parent().on("click", function () {
alert("click fired");
});
我搜索了很多页面:我找到了一个漂亮的解决方案。一探究竟:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<link href="https://gitcdn.github.io/bootstrap-toggle/2.2.2/css/bootstrap-toggle.min.css" rel="stylesheet">
<script src="https://gitcdn.github.io/bootstrap-toggle/2.2.2/js/bootstrap-toggle.min.js"></script>
<script>
$(function() {
$("#my_launch_today_chk").change(function() {
var chk = $(this).prop('checked');
if(chk == true){
console.log("On");
}else{
console.log("OFF");
}
});
});
</script>
</head>
<body >
<input type="checkbox" id="my_launch_today_chk" checked data-on="Launch" data-off="OFF" data-toggle="toggle" data-size="small">
</body>
</html>