我正在使用asp.net webform并尝试使我的复选框看起来像这样:
http://bootsnipp.com/snippets/featured/jquery-checkbox-buttons
我试图将html和js代码从上面的链接复制到我的页面但是它不起作用,可能缺少CSS等......
我需要添加到我的页面以使复选框看起来像上面的图片,有人可以提供完整的示例(包括html,css和脚本)吗?
这是上面链接中的代码:它甚至在这里工作:http://jsfiddle.net/DcYhf/65/
但是我将它复制到我的页面它不再有用了......
HTML
<span class="button-checkbox">
<button type="button" class="btn btn-lg" data-color="primary">Primary</button>
<input type="checkbox" class="hidden" checked />
</span>
JS
$(function () {
$('.button-checkbox').each(function () {
// Settings
var $widget = $(this),
$button = $widget.find('button'),
$checkbox = $widget.find('input:checkbox'),
color = $button.data('color'),
settings = {
on: {
icon: 'glyphicon glyphicon-check'
},
off: {
icon: 'glyphicon glyphicon-unchecked'
}
};
// Event Handlers
$button.on('click', function () {
$checkbox.prop('checked', !$checkbox.is(':checked'));
$checkbox.triggerHandler('change');
updateDisplay();
});
$checkbox.on('change', function () {
updateDisplay();
});
// Actions
function updateDisplay() {
var isChecked = $checkbox.is(':checked');
// Set the button's state
$button.data('state', (isChecked) ? "on" : "off");
// Set the button's icon
$button.find('.state-icon')
.removeClass()
.addClass('state-icon ' + settings[$button.data('state')].icon);
// Update the button's color
if (isChecked) {
$button
.removeClass('btn-default')
.addClass('btn-' + color + ' active');
}
else {
$button
.removeClass('btn-' + color + ' active')
.addClass('btn-default');
}
}
// Initialization
function init() {
updateDisplay();
// Inject the icon if applicable
if ($button.find('.state-icon').length == 0) {
$button.prepend('<i class="state-icon ' + settings[$button.data('state')].icon + '"></i> ');
}
}
init();
});
});
这就是我现在所拥有的:
答案 0 :(得分:1)
我认为有一个有效的例子:
$(function () {
$('.button-checkbox').each(function () {
// Settings
var $widget = $(this),
$button = $widget.find('button'),
$checkbox = $widget.find('input:checkbox'),
color = $button.data('color'),
settings = {
on: {
icon: 'glyphicon glyphicon-check'
},
off: {
icon: 'glyphicon glyphicon-unchecked'
}
};
// Event Handlers
$button.on('click', function () {
$checkbox.prop('checked', !$checkbox.is(':checked'));
$checkbox.triggerHandler('change');
updateDisplay();
});
$checkbox.on('change', function () {
updateDisplay();
});
// Actions
function updateDisplay() {
var isChecked = $checkbox.is(':checked');
// Set the button's state
$button.data('state', (isChecked) ? "on" : "off");
// Set the button's icon
$button.find('.state-icon')
.removeClass()
.addClass('state-icon ' + settings[$button.data('state')].icon);
// Update the button's color
if (isChecked) {
$button
.removeClass('btn-default')
.addClass('btn-' + color + ' active');
}
else {
$button
.removeClass('btn-' + color + ' active')
.addClass('btn-default');
}
}
// Initialization
function init() {
updateDisplay();
// Inject the icon if applicable
if ($button.find('.state-icon').length === 0) {
$button.prepend('<i class="state-icon ' + settings[$button.data('state')].icon + '"></i> ');
}
}
init();
});
});
var new_checkbox_button = function(name,id){
return "<li><span class='button-checkbox'><button type='button' class='btn btn-sm btn-primary active' data-color='primary'><i class='state-icon glyphicon glyphicon-check'></i> "+name+"</button><input type='checkbox' class='hidden' checked=''></span></li>";
};
$("#add").click(function(){
$("#selected_targets").append(new_checkbox_button("lallalala","1111"));
});
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/themes/smoothness/jquery-ui.css" rel="stylesheet"/>
<script src="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
<link href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap-theme.min.css" rel="stylesheet"/>
<link href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p> Existing checkboxes</p>
<span class="button-checkbox">
<button type="button" class="btn" data-color="default">Default</button>
<input type="checkbox" class="hidden" checked />
</span>
<span class="button-checkbox">
<button type="button" class="btn" data-color="primary">Primary</button>
<input type="checkbox" class="hidden" checked />
</span>
<span class="button-checkbox">
<button type="button" class="btn" data-color="success">Success</button>
<input type="checkbox" class="hidden" checked />
</span>
<span class="button-checkbox">
<button type="button" class="btn" data-color="info">Info</button>
<input type="checkbox" class="hidden" checked />
</span>
<span class="button-checkbox">
<button type="button" class="btn" data-color="warning">Warning</button>
<input type="checkbox" class="hidden" checked />
</span>
<span class="button-checkbox">
<button type="button" class="btn" data-color="danger">Danger</button>
<input type="checkbox" class="hidden" checked />
</span>
<span class="button-checkbox">
<button type="button" class="btn" data-color="link">Link</button>
<input type="checkbox" class="hidden" checked />
</span>
<br/><br/><br/>
<p> New checkboxes</p>
<button id="add">click to add checkbox</button>
<ui id="selected_targets"></ui>