gpt4 book ai didi

javascript - 使用 Jquery 和 Bootstrap 的单选按钮

转载 作者:行者123 更新时间:2023-11-28 17:09:07 33 4
gpt4 key购买 nike

我这里有代码(基本上是从 Jquery Checkbox Button 修改而来,通过更改 JS 中的 $widget.find('input:radio')type = "radio"在 html 中。

(它基本上做的是,它使 radio 看起来比普通的 radio 框更好,更用户友好,我想要一个大按钮,这样触摸屏用户可能会有更好的体验)

enter image description here

HTML

<span class="button-checkbox">
<button type="button" class="btn btn-lg" data-color="primary">Option 1</button>
<input type="radio" class="hidden" name="group1" id="op1" checked />
</span>

<span class="button-checkbox">
<button type="button" class="btn btn-lg" data-color="primary">Option 2</button>
<input type="radio" class="hidden" name="group1" id="op2" />
</span>

JS

$(function () {
$('.button-checkbox').each(function () {

// Settings
var $widget = $(this),
$button = $widget.find('button'),
$checkbox = $widget.find('input:radio'),
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();
});
});

这是演示:http://jsfiddle.net/9o41oykp/2/

问题:选中/未选中样式似乎有效,但因为这是 radio ,所以我需要它是互斥的,这意味着只能选中 1 个框(当你选中一个单选框时,其余的(在同一组中)应该自动取消选中),但是现在你看到你可以选中多个框(显示它的样式),我需要 jQuery 函数的帮助才能使其工作.

最佳答案

我更新了代码以使用单选按钮。问题是,当它与复选框一起使用时它会起作用,因为您可以修改单个复选框而不必担心 sibling ,但是对于单选按钮,您必须遍历所有 sibling ,选中/取消选中每一个,这需要循环每次进行更改时设置。

jsFiddle example

(请注意,在 fiddle 中,我暴露了单选按钮,因此您可以看到每个单选按钮是否被选中。)

$(function () {
$('.button-checkbox').each(function () {
// Settings
var $widget = $(this),
$button = $widget.find('button'),
$checkbox = $widget.find('input:radio'),
color = $button.data('color'),
settings = {
on: {
icon: 'glyphicon glyphicon-check'
},
off: {
icon: 'glyphicon glyphicon-unchecked'
}
};

// Event Handlers
$button.on('click', function () {
$checkbox.triggerHandler('change');
if ($(this).hasClass('btn-default')) {
$checkbox.prop('checked', !$checkbox.is(':checked'));
updateDisplay();
}
});

// Actions
function updateDisplay() {
$('.button-checkbox').each(function () {
var isChecked = $(this).find('input:radio').is(':checked');
// Set the button's state
$(this).find('button').data('state', (isChecked) ? "on" : "off");
// Set the button's icon
$(this).find('button').find('.state-icon')
.removeClass()
.addClass('state-icon ' + settings[$(this).find('button').data('state')].icon);
// Update the button's color
if (isChecked) {
$(this).find('button')
.removeClass('btn-default')
.addClass('btn-' + color + ' active');
} else {
$(this).find('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();
});
});

关于javascript - 使用 Jquery 和 Bootstrap 的单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29630174/

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