gpt4 book ai didi

javascript - 使用 jQuery 和 Bootstrap 设计复选框按钮的样式

转载 作者:太空宇宙 更新时间:2023-11-04 12:02:55 24 4
gpt4 key购买 nike

我正在使用 asp.net webform 并试图让我的复选框看起来像这样:

http://bootsnipp.com/snippets/featured/jquery-checkbox-buttons

enter image description here

我试图将上面链接中的 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();
});
});

这是我现在拥有的:

enter image description here

最佳答案

我认为有一个工作示例:

$(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>&nbsp;"+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>

关于javascript - 使用 jQuery 和 Bootstrap 设计复选框按钮的样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29614887/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com