【发布时间】:2015-04-13 20:52:07
【问题描述】:
我正在使用 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();
});
});
这就是我现在拥有的:
【问题讨论】:
-
我认为有一个可行的例子:jsfiddle.net/ivawzh/DcYhf/4
-
@Legends 它在这里工作:jsfiddle.net/DcYhf/65 但是我如何让它在我的网络表单上工作,需要什么参考?
-
看上面的HTML部分,里面都有引用。为了保护自己,不知道是不是真的需要jQuer UI,自己去看看吧。
标签: javascript jquery css asp.net twitter-bootstrap